简体   繁体   中英

Resizing a QPixmap inside a QLabel according to the Window

In my window class, inheriting of QMainWindow, I have a QLabel containing a QPixmap, updated every 20ms.

I want the QLabel, and the QPixmap inside it, to be resized according to the resizing of the window.

I want this Central Widget to take as much space as neccessary but also to be able to resize it down. Even smalled than the original size. But always keeping the ratio.

The actual code :

// in the window constructor
this->setWindowFlags(Qt::Window);
this->resize(500, 300);

this->setCentralWidget(this->label);

// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
this->label->setPixmap(output);

Now I've tried with :

output.scaled(this->label->x(), this->label->y(), Qt::KeepAspectRatio)

but it doesn't work ... How can I do that ?

EDIT : I'm using Qt 5.3

QPixmap::scaled is const. Next code doesn't work:

// in the window constructor
this->setCentralWidget(this->label);

// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
output.scaled( this->label->x(), this->label->y(), Qt::KeepAspectRatio );
this->label->setPixmap(output);

Because output doesn't change. Maybe you need something like this:

// in the window constructor
this->setCentralWidget(this->label);

// in the updating function
QPixmap output;
output = output.fromImage(Mat2QImage(theImage));
output = output.scaled( this->label->x(), this->label->y(), Qt::KeepAspectRatio );
this->label->setPixmap(output);

Try puting your QLabel inside a Layout. hl = new QHBoxLayout; hl->addWidget(label); centralWidget()->setLayout(hl);

check this out: http://qt-project.org/doc/qt-4.8/layout.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM