Dynamically filled QScrollArea does not resize its parent

  • Hello all. Since today, I was just a reader and I never feel a need to get an account and ask or answer a question (usually somebody already did that for me, so I just need to look for an answer long enough).

    To the point. I want to create simple dialog with QComboBox that is always visible. Below it there is a container for a dynamically created widgets that will be put there when QComboBox index will change. Because I don't know how many of them will be there I decided that there will be a QScrollArea which will allow to scroll if QDialog will be to tall to be displayed properly (to many widgets to be seen at once).
    On the other hand if the number of items will be to small to scroll up and down it should shrink to match contents.
    I spend couple hours with different approaches and still I am not able to resize QScrollArea.

    The best I got was a proper resize to fit, but without a shrink (widgets are spaced evenly) when I implemented sizeHint for a QScrollArea. Any ideas how to solve that problem? Thank you in advance.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the code you are using ?

  • Thank you for response. I put sample code in the repository https://github.com/bialasjaroslaw/QScrollArea

  • Lifetime Qt Champion

    Just one point I'm not sure to follow. You would like to shrink your dialog if the overall size of the number of combobox shown is small than the current dialog, correct ?

  • Exactly. I probably have not done it, but I would set QDialog minimum/maximum size and if QScrollArea is not able to present them all, then I assume that scrolling will be the only option. On the other hand if contents will be smaller then max size, then QDialog should shrink to fit to contents, but no more than its minimum size (second part is rather optional if it will be too much/too complicated to achieve).

    In presented case QDialog does not have any size limits and even if I change its sizePolicy, it will not cause any difference (at least I am not able to see anything different). It stays the same in case where there is one item or dozen.

  • Lifetime Qt Champion

    What you can do is to use a single shot QTimer with a small value to trigger a slot that will check the sizeHint of scrollAreaWidgetContents and apply the changes you want based on it. Start the timer in addItems.

  • That solved my problem. Thank you a lot.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.