I'm creating a program (in Qt Creator 2.8.1, Qt 5.1.1) that basically shows an image, in this case a playing card (along with a few buttons, labels, and a line edit). All widgets are in vertical/horizontal layouts, and the window layout is a grid layout.
I reimplemented the main window's resize event to get the image to resize correctly and ajust the pixmap to it's size - basically, the label expands vertically as much as it can (vertical size policy set to Expand(1)), and then the image is rescaled.
When the window is expanded, everything works fine, and both the label and the image resize correctly. However, I can't shrink the window back: that is, when resizing, I can't get the window to have a smaller height than necessary to include the current label size - neither the label nor the image resize. If I shrink the window horizontally, so that, in order to keep the ratio, the image is shrunk (left-most image), then I can shrink the window vertically. Note that, when resizing the window horizontally, only the image/pixmap is shrunk, not the label.
Here is the code that I'm using to manage the sizes(2):
void MainWindow::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);
//Some code that is not causing the problem - I've checked
showImage();
}
void MainWindow::showImage()
{
int w = ui->imageLabel->width();
int h = ui->imageLabel->height();
//Getting image path from file - also not causing the problem
QPixmap pixmap(":/image/path.png");
//The image is quite big, so I need to
// set a scaled pixmap to a w x h window, keeping its aspect ratio
ui->imageLabel->setPixmap(pixmap.scaled(w,h,Qt::KeepAspectRatio));
ui->imageLabel->setMask(pixmap.mask());
}
And here is the arrangement in Qt Designer and when running:
So, to recap:
What I find weird is that the pixmap prevents the window from resizing vertically, but has no problem in shrinking if the window is resized horizontally.
The question is, as it could only be: any ideas on how to solve this?
(1) Also tried Minimum Expanding, same thing happened. - Also tried resizing the label programatically, but then the other widgets ignore the label's size and don't move, causing overlapping.
(2) Since I am using Qt for pretty much the first time, I can't/don't know how to copy an amount of code that can be copied, compiled and executed, without filling this question with lots of unimportant code.
Note: Feel free to ask for more information that you think may be useful in finding the problem's cause and/or solution.
You can scale the pixmap by keeping its aspect ratio every time it changes: QPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.
Once you have add your layout with at least one widget in it, select your window and click the "Update" button of QtDesigner. The interface will be resized at the most optimized size and your layout will fit the whole window. Then when resizing the window, the layout will be resized in the same way.
For people still looking for a solution, you need to set the minimum size for the QLabel:
ui->imageLabel->setMinimumSize(1, 1);
The solution is pointed out here: https://forum.qt.io/topic/58749/solved-how-to-shrink-qmainwindow-with-a-qlabel-including-an-image/3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With