Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Designer - window won't get smaller than a QLabel with pixmap

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: Arrangement

So, to recap:

  1. When increasing the windows's height, the label and image grow accordingly, keeping its ratio.
  2. When decreasing the windows's height:
    • If the image doesn't have to change, even if the label does, the window resizes normally.
    • If the image needs to shrink, then the window doesn't resize at all.
  3. When decreasing the windows's width, the window resizes normally
    • If necessary to keep the image ratio, the image(pixmap) shrinks, but the label only shrinks horizontally

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.

like image 515
Sampaio Avatar asked Jul 24 '14 17:07

Sampaio


People also ask

How do you scale a pixmap?

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.

How do I resize a layout in Qt?

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.


1 Answers

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

like image 110
mahesh Avatar answered Oct 13 '22 20:10

mahesh