1
votes

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.

I want to set the size hint for the QDialog such that the dialog is just large enough that the scroll bars for the scroll area are not visible when the dialog is first shown (i.e. ensure size of QScrollArea viewport = size hint of child widget in scroll area). Currently, the default sizeHint() implementation for the QDialog has insufficient height, which causes the vertical scroll bar to be shown when first loaded.

I thought this could be achieved by re-implementing sizeHint() for the QDialog, whereby the size hint of the dialog would be adjusted by the amount required for the size of QScrollArea viewport to equal the size hint for child widget in the scroll area (for the first page of the stacked layout). Unfortunately, in sizeHint(), the size of the QScrollArea viewport is set to the default size of QStackedWidget (640x480), and only updates to the correct size once the QDialog is shown.

Is there some way to get the correct size of the QScrollArea viewport before it is shown, or another way to achieve the desired effect of adjusting the size hint of the dialog to prevent scroll bars from being shown when it is first displayed (aside from hard-coding the dialog size).

1

1 Answers

1
votes

With the composition of your dialog as:

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.

The tricky part is to answer:

Is there some way to get the correct size of the QScrollArea viewport before it is shown?

Well, before switching to certain page you can estimate the scroll area viewport if it is either correctly set or you can just measure the content going inside the scrollarea. I usually force the widget to demand certain height from the scroll area like that:

wdgetInScrollArea->setMinimumSize( widgetInScrollArea->sizeHint() );
wdgetInScrollArea->adjustSize(); // sometimes it is needed

The the scroll area viewport hint is then more 'adequate':

qDebug() << scrollArea->viewPortSizeHint(); // report

I don't see the code but usually it is not even required to do any custom event handling here, just prepare all the nested widgets like that.