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

荒凉一梦 提交于 2021-01-02 05:38:27

问题


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.


回答1:


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




回答2:


First of all, to change the label size, you must select an appropriate size policy for the label like expanding or minimum expanding. Then you must scale the pixmap by keeping its aspect ratio every time it changes, for example in the resizeEvent of the widget that contains your label.

void Widget::resizeEvent(QResizeEvent* event)
{
    QLabel::setPixmap(pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

To keep specific aspect ratio reimplement sizeHint() function, or better yet, use setHeightForWidth(true) and reimplement heightForWidth() function. And don't forget, that your label placed in CentralWidget, but not directly in MainWindow.



来源:https://stackoverflow.com/questions/24940709/qt-designer-window-wont-get-smaller-than-a-qlabel-with-pixmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!