小编Sam*_*aio的帖子

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

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

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

扩展窗口后,一切正常,标签和图像均正确调整大小。但是,我无法将窗口向后收缩:也就是说,在调整大小时,我无法使窗口的高度小于包含当前标签大小所需的高度-标签和图像的大小均无法调整。如果我水平缩小窗口,以便保持比例,图像会缩小(最左边的图像),那么我可以垂直缩小窗口。请注意,在水平调整窗口大小时,仅缩小图像/像素图,而不缩小标签。

这是我用来管理size(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());       
}
Run Code Online (Sandbox Code Playgroud)

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

c++ qt qt-creator qt-designer

6
推荐指数
2
解决办法
2556
查看次数

标签 统计

c++ ×1

qt ×1

qt-creator ×1

qt-designer ×1