Search code examples
qtqscrollareaqlayoutqhboxlayout

QScrollArea widget auto resize when adding child widgets


I have the following structure:

QScrollArea
  - QWidget (with QHBoxLayout)
    - QLabel
    - QLabel
    - QLabel
    ...

QLabels are generated automatically with random text.

How do I change the size of the widget automatically to fit labels?

At the moment, I can see the widget only by setting a fixed size.


Solution

  • Use resize() method on the widget.

    resize(sizeHint())