Important: Please read the Qt Code of Conduct -

Scrollbar appearing/disappearing infinitely -> override calculation used to display scrollbar in QScrollArea

  • To better explain my issue, let me describe my situation. I have a QScrollArea with a bunch of widgets that expand both vertically and horizontally while maintaining their aspect ratio using heightForWidth. This means that if their width is increased, their height will too. If their width is decreased, so will their height.

    If my QScrollArea is barely smaller than the height of all my widgets once they have expanded vertically, the vertical scrollbar will appear. This reduces the width available to my widgets and their height will in turn decrease. Now, all the widgets fit inside the viewport again and the scrollbar is removed. More space is available to the widgets and they expand out of the viewport. The scrollbar appears again.... This process keeps repeating and the scrollbar appears and disappears infinitely, constantly resizing my widgets.

    I'm looking for a proper solution to this problem other than having the scrollbar always visible.

    I found that there is a function in QAbstractScrollArea called maximumViewportSize but it's unfortunately not a virtual function. For a test, I manually overrode resizeEvent which calls updateScrollbars (also not virtual), which in turn calls maximumViewPortSize. I made my own maximumViewportSize that subtracted the width of a scrollbar to the viewport's width calculation and it worked perfectly. Unfortunately, this required reimplementing a lot of functions and is not very maintainable.

    Is there a better solution here?

    I can provide a MVCE if necessary.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be a silly idea but can't you force the resizing to happen such as their total height passes the threshold that triggers your problem ?

  • Sorry, I'm not sure if I understand your response... What resizing are you referring to? The resizing of the viewport or of the widgets?

  • Lifetime Qt Champion

    I was thinking about the widgets.

    By the way, how are you adding them to the QScrollArea ?

Log in to reply