Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to control QListWidget height depending on the presence of horizontal scrollbar?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 6.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roni219
    wrote on last edited by roni219
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • R roni219

      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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roni219
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roni219
            wrote on last edited by roni219
            #5

            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.

            R 1 Reply Last reply
            0
            • R roni219

              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.

              R Offline
              R Offline
              roni219
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved