Important: Please read the Qt Code of Conduct -

Resizing QScrollArea to exactly fit contents

  • I've built a program which shows a QScrollArea containing a custom widget. I would like it to make it so that when the program launches, the QScrollArea is exactly the right size to show the contained widget with no scroll bars. But if I try scr.resize(clock.sizeHint()), it's just a little too small and I see scrollbars. How can I avoid this? The gist of my code is below:

    AnalogClock clock;
    QScrollArea scr;

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should rather trigger the resize after it's shown using e.g. a QTimer with a 0 timeout otherwise the sizeHint might give you something not accurate since the widget you are getting the hint from is still invisible.

    Hope it helps

Log in to reply