2
votes

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

2

2 Answers

2
votes

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);
0
votes

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