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() :
        QListWidget(0)
    {
        setWrapping(false);
        setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
        setViewMode(QListWidget::IconMode);
        setResizeMode(QListWidget::Adjust);
        
        int height = 76; // height of an item + height of scrollbar
        setFixedHeight(height);
    }
    

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

    https://drive.google.com/open?id=0B3VIzaAFexv9ODhmYmswbUlyXzA

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

    https://drive.google.com/open?id=0B3VIzaAFexv9bi1qdWFkYkJIams

    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!

    Roni.


  • Qt Champions 2016

    Hi,
    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.



  • what you want is probably

    setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    

    instead of

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


  • 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:

    https://drive.google.com/open?id=0B3VIzaAFexv9d3BnLTFJT0JfRmc

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

    https://drive.google.com/open?id=0B3VIzaAFexv9VW16X2FDMDRLOGc

    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?

    Thanks!


Log in to reply
 

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