How to control QListWidget height depending on the presence of horizontal scrollbar?

  • Hello,

    I use QListWidget to preview the row of icons with text captions.

    MyWidget::MyWidget() :
        int height = 76; // height of an item + height of scrollbar

    The code sets fixed height for the entire widget. It looks good when the horizontal scrollbar is present:

    When widget is resized to accommodate all items, scrollbar disappears (as expected). But the widget has extra white space below the items:

    Instead I would like to have a fixed viewport height. The entire widget height should change when the widget is resized. It must depend on the presence absence of toolbar as

    Without toolbar: widget height == item height
    With toolbar: widget height == item height + toolbar height

    How can this be done?

    Thank you in advance!


  • Qt Champions 2017

    You can install an event filter to the scrollbar and capture the show/hide events and then in the event filter you can adjust your widget's size accordingly.

    Kind regards.

  • Thank you for the prompt response, @kshegunov! I will try out your suggestion, and post the result.

  • Qt Champions 2018

    what you want is probably

    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

    instead of

    int height = 76; // height of an item + height of scrollbar

  • Thank you for the suggestion, @VRonin. I tried the fix. The widget shrunk horizontally, instead of taking the entire width allowed it in the layout:

    When toolbar was present the bottom part of the caption was obscured:

    The first issue is fixed by using

    setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);

    But the indexes are partially obscured by toolbar. Or almost entirely obscured in non-zero spacing is set to the widget.

  • Can anybody comment, please? To summarise, the QAbstractScrollArea::AdjustToContents method, proposed by @VRonin, is simple and almost works:

    The hight adjusts to the icons, but not to the icons+text underneath them.

    Is it possible to make it adjust so that both icons and text is visible?


Log in to reply

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