Get size of QScrollArea viewport before showing

  • 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 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)?

  • Lifetime Qt Champion

    Hi and welcome

    Have you tried with virtual
    void Mydialog::showEvent ( QShowEvent * e)
    // do what u need

    It its called just before the dialog is shown so all sizes should be set.

  • @mrjj Your solution worked perfectly. Thanks!

Log in to reply