Reputation: 325
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. I resize 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'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)
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;
};
Upvotes: 3
Views: 1106
Reputation: 311
I have faced a similar problem some months ago (with a Qt SQL table view).
In short: try adding a CentralWidget->adjustSize(); line before adjusting the MainWindow's size.
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.
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. 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.
[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). In case of using such an "auto-resizing" widget, adjusting the CentralWidget's size can be omitted.
Upvotes: 1