Once again about resizing a window to the size of its children in QT

A similar question has already been asked, but didn't give a clear answer, so I'll ask again. Let's say we have a QMainWindow and a QScrollArea inside. I am resizing the QScrollArea in the program and I want the window to resize accordingly. The following code works almost correctly: when the new image is larger than the old one, the window size increases. However, when the new image is smaller, the window gets smaller, instead only the QScrollArea gets small and large spaces appear between the QScrollArea and other elements (label, buttons)

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

      

+3


source to share


1 answer


I ran into a similar problem a few months ago (with a Qt SQL table view).

In short : try adding CentralWidget-> adjustSize (); line to by adjusting the size of MainWindow.

Example:

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

      



Explanation . In my case, the key factor was that I used the MainWindow + CentralWidget combination to present the UI.

When you try to adjust the size of the "MainWindow" "CentralWidget" to the size of your content, the center size will be equal to the size of the content. In these situations, the MainWindow adjustSize () method tries to resize the window to the CentralWidget, but the CentralWidget still has an original size larger than [1], so MainWindow retains its original size.

[1]: Some widgets can automatically resize (I can't remember in particular, but I'm sure there are some), but in your code you are using a simple QWidget as CentralWidget and QWidgets lack this capability (like QMainWindows) ... In the case of using such an auto-resizing widget, you can reduce the size of the centralized scaling.

+1


source







All Articles