Problem with using QScrollArea + QStackedWidget + wordwrapped QLabel.
-
Hi, I have some problem and I can't understand why is it happens.
I've gotQScrollArea
withwidgetResizable
set to true.
InsideQScrollArea
theQStackedWidget
is placed which contains multiple pages.I connected to
QStackedWidget::currentChanged(int)
signal in order to handle switching between tabs.
ThecurrentChanged(int)
handler looks like this:void MainWidget::OnCurrentChanged(int index) { QWidget* currentWidget = ui->stackedWidget->widget(index); ui->stackedWidget->setMinimumSize(currentWidget); }
Why do I need such code? Because of
QScrollArea
, by defaultQStackedWidget
gets thesizeHint()
of the biggest widget, in other words:
(pseudocode)stackedSizeHint.width = max( contained_widgets.width ) stackedSizeHint.height = max( contained_widgets.height )
As I said before,
QStackedWidget
is placed insideQScrollArea
, so without the code above,QScrollArea
would have scrolls permanently if one of the widgets needs it.
That's why I wrotecurrentChanged(int)
handler.Every page of my
QStackedWidget
contains only simpleQPushButton
,QLabel
and vertical spacer, and all of them are laid out inside vertical layout.
Everything worked as expected, but the problem appeared when I turned onwordWrap
inQLabel
. After settingQLabel
'swordWrap
to true, strange thing happens - useless scroll appears every time when it's not needed at all (on page which contains QLabel withwordWrap
enabled).
Why? How can I fix it?I also tried to print the widgets geometry, scroll appears because of spacer which somehow keeps his size unnecessary big.
Why does it happen? Is there a solution for this problem?
Thanks.
-
Hi,
Rather than adding a spacer item, you can add a stretch after your widgets which should keep them in place and not use more space.
Hope it helps
-
If you are talking about QSpacerItem, then yes you are right but sizeHint = (0,0) doesn't look right.
Anyway can you show a minimal sample code that shows this behavior ?