Qt Designer - 窗口不会比带有像素图的 QLabel 小 [英] Qt Designer - window won't get smaller than a QLabel with pixmap

查看:73
本文介绍了Qt Designer - 窗口不会比带有像素图的 QLabel 小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序(在 Qt Creator 2.8.1、Qt 5.1.1 中),它基本上显示图像,在本例中是一张扑克牌(以及一些按钮、标签和行编辑).所有widget都是垂直/水平布局,窗口布局是网格布局.

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.

我重新实现了主窗口的 resize 事件 以使图像正确调整大小并将像素图调整为它的大小 - 基本上,标签尽可能垂直扩展(垂直大小策略设置为 Expand(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.

当窗口展开时,一切正常,标签和图像都正确调整大小.但是,我无法将窗口缩小:也就是说,在调整大小时,我无法使窗口的高度小于包含当前标签大小所需的高度 - 标签和图像都不会调整大小.如果我水平缩小窗口,为了保持比例,图像会缩小(最左边的图像),然后我可以垂直缩小窗口.请注意,当水平调整窗口大小时,只有图像/像素图会缩小,标签不会.

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.

这是我用来管理尺寸的代码(2):

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());       
}

这是 Qt Designer 和运行时的安排:

And here is the arrangement in Qt Designer and when running:

所以,回顾一下:

  1. 当增加窗口的高度时,标签和图像会相应地增长,并保持其比例.
  2. 当降低窗户的高度时:
    • 如果图像不必更改,即使标签更改,窗口也会正常调整大小.
    • 如果图像需要缩小,则窗口根本不会调整大小.
  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.
  • 如果需要保持图像比例,图像(pixmap)会缩小,但标签只会水平缩小

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

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

(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) 因为我几乎是第一次使用 Qt,所以我不能/不知道如何复制大量可以复制、编译和执行的代码,而不用大量填充这个问题不重要的代码.

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

推荐答案

对于仍在寻找解决方案的人,您需要设置 QLabel 的最小尺寸:

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

ui->imageLabel->setMinimumSize(1, 1);

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

这篇关于Qt Designer - 窗口不会比带有像素图的 QLabel 小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆