简体   繁体   English

再次关于在QT中将窗口大小调整为其子项的大小

[英]Once again about resizing window to the size of its children in QT

A similar question has already been asked, but no clear answer was given, so I'll ask again. 已经提出了一个类似的问题,但没有给出明确的答案,所以我再问一遍。 Say we have a QMainWindow, and a QScrollArea inside. 假设我们有一个QMainWindow和一个QScrollArea。 I resize the QScrollArea in the program and I want the window to resize accordingly. 我在程序中调整QScrollArea的大小,我希望窗口相应地调整大小。 The following code works almost correctly: when the new image is larger than the old one, the window's size increases. 以下代码几乎正常工作:当新图像大于旧图像时,窗口的大小会增加。 However, when the new image is smaller, the window doesn't become smaller, instead, only the QScrollArea becomes small and large spaces appear between the QScrollArea and other elements (label, buttons) 但是,当新图像较小时,窗口不会变小,相反,只有QScrollArea变小,QScrollArea和其他元素(标签,按钮)之间出现大空格

class PictureDialog : public QMainWindow {
Q_OBJECT

public:
    PictureDialog() : QMainWindow() {
        QWidget* canvas = new QWidget(this);
        setCentralWidget(canvas);
        QVBoxLayout* layout = new QVBoxLayout(canvas);
        imageLabel = new QLabel(" ");
        imageLabel->setStyleSheet("QLabel { background-color : white; color : black; }");
        scrollArea = new QScrollArea(this);
        scrollArea->resize(300, 300);
        scrollArea->setBackgroundRole(QPalette::Dark);
        scrollArea->setWidget(imageLabel);
        layout->addWidget(scrollArea);
        imgnamelabel = new QLabel(tr("Picture: "), this);
        layout->addWidget(imgnamelabel);
        QHBoxLayout *hlayout = new QHBoxLayout();
        layout->addLayout(hlayout);
        yesButton = new QPushButton(QPixmap(":pics/yes.png"), QString::null, this);
        yesButton->setShortcut(Qt::Key_Plus);
        yesButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
        hlayout->addWidget(yesButton);
        noButton = new QPushButton(QPixmap(":pics/no.png"), QString::null, this);
        noButton->setShortcut(Qt::Key_Minus);
        noButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
        hlayout->addWidget(noButton);
        hlayout->addStretch();
        connect(yesButton, SIGNAL(clicked()), SLOT(hide()));
        connect(noButton, SIGNAL(clicked()), SLOT(hide()));
    }

    void setPicture(QString imagePath, bool showNo) {
        imgnamelabel->setText(tr("Picture: ") + imagePath);
        if (!QFile::exists(imagePath)) {
            imageLabel->setText(tr("Picture file not found: ") + imagePath);
            imageLabel->resize(imageLabel->fontMetrics().boundingRect(imageLabel->text()).width(),
                               imageLabel->fontMetrics().boundingRect(imageLabel->text()).height());
        } else {
            QImage image(imagePath);
            if (image.isNull()) {
                imageLabel->setText(tr("Failed to open picture file: ") + imagePath);
                imageLabel->resize(imageLabel->fontMetrics().boundingRect(imageLabel->text()).width(),
                                imageLabel->fontMetrics().boundingRect(imageLabel->text()).height());
            } else {
                imageLabel->setPixmap(QPixmap::fromImage(image));
                imageLabel->resize(image.width(), image.height());
            }
        }

        scrollArea->setFixedSize(mini(imageLabel->width() + 20, QApplication::desktop()->screenGeometry().width() * 8 / 10),
                                 mini(imageLabel->height() + 20, QApplication::desktop()->screenGeometry().height() * 8 / 10));
        adjustSize();
        updateGeometry();
        if (showNo)
            noButton->setEnabled(true);
        else
            noButton->setEnabled(false);
    }

    QPushButton *yesButton, *noButton;

private:
    QLabel *imageLabel;
    QLabel *imgnamelabel;
    QScrollArea* scrollArea;
};

I have faced a similar problem some months ago (with a Qt SQL table view). 几个月前我遇到过类似的问题(使用Qt SQL表视图)。

In short : try adding a CentralWidget->adjustSize(); 简而言之 :尝试添加CentralWidget-> adjustSize(); line before adjusting the MainWindow's size. 调整MainWindow的大小之前的行。

Example: 例:

...
scrollArea->setFixedSize(...);
canvas->adjustSize();
adjustSize();
updateGeometry();
...

Explanation : In my case the key factor was that i have been using the MainWindow + CentralWidget combination for presenting the UI. 说明 :在我的例子中,关键因素是我一直在使用MainWindow + CentralWidget组合来呈现UI。

When you try to adjust a "CentralWidgeted" MainWindow's size to the size of its content, it will take the CentralWidget's size as content size. 当您尝试将“CentralWidgeted” MainWindow的大小调整为其内容大小时,它将使CentralWidget的大小作为内容大小。 In such situations the MainWindow's adjustSize() method tries to resize the window to the CentralWidget, but the CentralWidget still has the original - larger - size[1], thus the MainWindow keeps its original size. 在这种情况下,MainWindow的adjustSize()方法尝试将窗口的大小调整为CentralWidget,但CentralWidget仍然具有原始的 - 较大的[1],因此MainWindow保持其原始大小。

[1]: Some widgets might be able to resize themselves automatically (i can't recall any in particular, but i'm sure there are some), but in your code you use a simple QWidget as CentralWidget and QWidgets lack this capability (just like QMainWindows). [1]:一些小部件可能能够自动调整大小(我不记得任何特别的,但我确定有一些),但在你的代码中你使用一个简单的QWidget作为CentralWidget和QWidgets缺乏这种能力(就像QMainWindows一样)。 In case of using such an "auto-resizing" widget, adjusting the CentralWidget's size can be omitted. 在使用这种“自动调整大小”小部件的情况下,可以省略调整CentralWidget的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM