I'm using Qt 5.15 with C++17. I have a hierarchy of Widgets like hinted in the title:
->children() = [
InnerWidget,
InnerWidget,
InnerWidget,
...
]
MainWindow : QMainWindow
->centralWidget() = QScrollArea
->widget() = QWidget
->layout() = QVBoxLayout
->children() = [
InnerWidget,
InnerWidget,
InnerWidget,
...
]
To copy to clipboard, switch view to plain text mode
with
InnerWidget.children() = [ QLabel, QLabel ]
To copy to clipboard, switch view to plain text mode
.
I have set
InnerWidget::setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)
To copy to clipboard, switch view to plain text mode
inside InnerWidget's constructor.
The QLabels inside InnerWidget are initialized with some QPixmap like
label->setPixmap(img);
label->setFixedSize(img.size());
label->setPixmap(img);
label->setFixedSize(img.size());
To copy to clipboard, switch view to plain text mode
_________________
This all works fine, however now I added an option to zoom all the pixmaps to the MainWindow's menubar. I connect to a signal MainWindow::zoom_inner(.., which itself calls InnerWidget::zoom(.. on every InnerWidget, which resizes the images / labels like this:
auto scaled = img.scaled(img.size() * factor);
label->setPixmap(scaled);
label->setFixedSize(scaled.size());
auto scaled = img.scaled(img.size() * factor);
label->setPixmap(scaled);
label->setFixedSize(scaled.size());
To copy to clipboard, switch view to plain text mode
Finally I update the size of the MainWindow by finding the maximum InnerWidget.frameGeometry().width() (= inner_max_width) and calling
void MainWindow::zoom_inner() {
// ...
setMaximumWidth(inner_max_width + centralWidget()->verticalScrollBar()->width() + 1);
resize(maximumWidth(), size().height());
}
void MainWindow::zoom_inner() {
// ...
setMaximumWidth(inner_max_width + centralWidget()->verticalScrollBar()->width() + 1);
resize(maximumWidth(), size().height());
}
To copy to clipboard, switch view to plain text mode
_________________
Now after calling MainWindow::zoom(.., the window seems to resize to the correct size, as do the QLabel's inside the InnerWidget's. However, the InnerWidget's themselves do not resize at all, they just stay the same size as before and I have no idea why. I tried calling many combinations of
adjustSize();
update();
layout()->update();
updateGeometry();
adjustSize();
update();
layout()->update();
updateGeometry();
To copy to clipboard, switch view to plain text mode
on any of the MainWindow,QScrollArea,InnerWidget but nothing happens.. even calling
InnerWidget::resize(new_size)
InnerWidget::resize(new_size)
To copy to clipboard, switch view to plain text mode
has no effect. Clearly InnerWidget::sizeHint() gives the correct value, because the MainWindow size after MainWindow::zoom(.. is correct. So why do the InnerWidget's refuse to resize to fit the QLabel's, even though sizePolicy is set to QSizePolicy::Minimum?
This seems like some sort of misunderstanding on my part so I'm hoping the answer is simple and doesn't need a MWE. I've tried the docs but couldn't find a solution.
Bookmarks