Qt Designer - 窗口不会比带有pixmap的QLabel小

时间:2022-06-21 22:44:13

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.

我正在创建一个程序(在Qt Creator 2.8.1,Qt 5.1.1中),它基本上显示了一个图像,在这种情况下是一张扑克牌(以及一些按钮,标签和一行编辑)。所有小部件都是垂直/水平布局,窗口布局是网格布局。

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.

我重新实现了主窗口的resize事件,以使图像正确调整大小并将pixmap调整到它的大小 - 基本上,标签尽可能多地垂直扩展(垂直大小策略设置为Expand(1)),然后图像是重新调整。

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

这是我用来管理大小的代码(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: Qt Designer  - 窗口不会比带有pixmap的QLabel小

以下是Qt Designer中的安排以及运行时:

So, to recap:

所以,回顾一下:

  1. When increasing the windows's height, the label and image grow accordingly, keeping its ratio.
  2. 当增加窗户的高度时,标签和图像会相应增长,保持其比例。
  3. 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.
    • 如果图像需要缩小,则窗口根本不会调整大小。
  4. 降低窗口高度时:如果图像不必更改,即使标签有效,窗口也会正常调整大小。如果图像需要缩小,则窗口根本不会调整大小。
  5. 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
    • 如果需要保持图像比例,图像(像素图)会缩小,但标签只会水平缩小
  6. 当减小窗口宽度时,窗口会正常调整大小如果需要保持图像比例,图像(像素图)会缩小,但标签只会水平缩小

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.

我觉得奇怪的是pixmap会阻止窗口垂直调整大小,但如果窗口水平调整大小,则缩小没有问题。

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.

(1)也试过最小扩展,同样的事情发生了。 - 还尝试以编程方式调整标签大小,但随后其他小部件忽略标签的大小并且不移动,从而导致重叠。

(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.

(2)由于我第一次使用Qt,我不知道如何复制可以复制,编译和执行的代码量,而不用大量不重要的代码来填充这个问题。

Note: Feel free to ask for more information that you think may be useful in finding the problem's cause and/or solution.

注意:您可以随意询问您认为可能有助于查找问题原因和/或解决方案的更多信息。

2 个解决方案

#1


1  

For people still looking for a solution, you need to set the minimum size for the QLabel:

对于仍在寻找解决方案的人,您需要设置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

这里指出了解决方案:https://forum.qt.io/topic/58749/solved-how-to-shrink-qmainwindow-with-a-qlabel-including-an-image/3

#2


0  

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.

首先,要更改标签大小,必须为标签选择适当的大小策略,例如扩展或最小扩展。然后,您必须通过在每次更改时保持其纵横比来缩放像素图,例如在包含标签的窗口小部件的resizeEvent中。

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.

要保持特定的宽高比重新实现sizeHint()函数,或者更好,请使用setHeightForWidth(true)并重新实现heightForWidth()函数。并且不要忘记,您的标签放在CentralWidget中,但不是直接放在MainWindow中。

#1


1  

For people still looking for a solution, you need to set the minimum size for the QLabel:

对于仍在寻找解决方案的人,您需要设置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

这里指出了解决方案:https://forum.qt.io/topic/58749/solved-how-to-shrink-qmainwindow-with-a-qlabel-including-an-image/3

#2


0  

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.

首先,要更改标签大小,必须为标签选择适当的大小策略,例如扩展或最小扩展。然后,您必须通过在每次更改时保持其纵横比来缩放像素图,例如在包含标签的窗口小部件的resizeEvent中。

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.

要保持特定的宽高比重新实现sizeHint()函数,或者更好,请使用setHeightForWidth(true)并重新实现heightForWidth()函数。并且不要忘记,您的标签放在CentralWidget中,但不是直接放在MainWindow中。