Maryu
24th December 2021, 17:40
I'm using Qt 5.15 with C++17. I have a hierarchy of Widgets like hinted in the title:
MainWindow : QMainWindow
->centralWidget() = QScrollArea
->widget() = QWidget
->layout() = QVBoxLayout
->children() = [
InnerWidget,
InnerWidget,
InnerWidget,
...
]
with
InnerWidget.children() = [ QLabel, QLabel ].
I have set
InnerWidget::setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum) inside InnerWidget's constructor.
The QLabels inside InnerWidget are initialized with some QPixmap like
label->setPixmap(img);
label->setFixedSize(img.size());
_________________
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());
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());
}
_________________
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();
on any of the MainWindow,QScrollArea,InnerWidget but nothing happens.. even calling
InnerWidget::resize(new_size) 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.
MainWindow : QMainWindow
->centralWidget() = QScrollArea
->widget() = QWidget
->layout() = QVBoxLayout
->children() = [
InnerWidget,
InnerWidget,
InnerWidget,
...
]
with
InnerWidget.children() = [ QLabel, QLabel ].
I have set
InnerWidget::setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum) inside InnerWidget's constructor.
The QLabels inside InnerWidget are initialized with some QPixmap like
label->setPixmap(img);
label->setFixedSize(img.size());
_________________
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());
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());
}
_________________
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();
on any of the MainWindow,QScrollArea,InnerWidget but nothing happens.. even calling
InnerWidget::resize(new_size) 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.