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. Lost on resizing QListWidget :(
Forum Updated to NodeBB v4.3 + New Features

Lost on resizing QListWidget :(

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 1.2k Views 1 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.
  • jsulmJ jsulm

    @idlefrog said in Lost on resizing QListWidget :(:

    w.adjustSize();

    Try to call this after w.show()

    idlefrogI Offline
    idlefrogI Offline
    idlefrog
    wrote on last edited by idlefrog
    #4

    @jsulm
    Adding w.adjustSize() after w.show() has no effect. It looks exactly the same.

    1 Reply Last reply
    0
    • JonBJ JonB

      @jsulm
      I have a question on this, which I have never understood fully in Qt widgets. Instead of having to call adjustSize() on showing/resizing/whatever (and btw here, is it necessary to call adjustSize() in the show/resize event, not just after w.show() which only sets a flag for show), is it not possible to set a size policy which would make Qt do this for you automatically? Or, is this not possible because it's a QListWidget?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @JonB I think you're right. Calling show() does not yet show anything and so does not size the widgets.
      @idlefrog Try to play with sizePolicy of your widget.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      idlefrogI 1 Reply Last reply
      0
      • idlefrogI Offline
        idlefrogI Offline
        idlefrog
        wrote on last edited by
        #6

        Here's my brute force solution:

        #include <QApplication>
        #include <QListWidget>
        #include <QDebug>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            QListWidget w;    
            QStringList items = {"CO2 - Carbon Dioxide", "H2O - Water", "NaCl - Sodium Choride", "NH3 - Ammonia" };
            w.addItems(items);
        
            auto column_width = w.sizeHintForColumn(0);
            auto widget_size = w.sizeHint();
            auto frame_width = w.frameWidth();
            auto margins = w.contentsMargins();
            auto new_width = column_width+frame_width + margins.left()+margins.right();
            
            widget_size.setWidth(new_width);
            w.resize(widget_size);
        
            w.show();
        
            return a.exec();
        }
        

        Which looks like this:
        6f264df9-7c69-402d-b2cb-adb41e72c197-image.png

        I'll try the size policy next :)

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @JonB I think you're right. Calling show() does not yet show anything and so does not size the widgets.
          @idlefrog Try to play with sizePolicy of your widget.

          idlefrogI Offline
          idlefrogI Offline
          idlefrog
          wrote on last edited by
          #7

          @jsulm
          I've tried every size policy, but none make any useful difference.
          It seems that QListWidget does not support an 'adjust to fit contents'.

          There is a setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents), but that doesn't respect the size of the items in the list.

          1 Reply Last reply
          1
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #8

            This will do it.
            QFont font = w->font(); /*get default font */
            QRectF fontBoundRect = QFontMetrics( font ).tightBoundingRect( the longst string );
            w->setFixedWidth( fontBoundRect.width() );
            sure you may need some margins.

            idlefrogI 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              This will do it.
              QFont font = w->font(); /*get default font */
              QRectF fontBoundRect = QFontMetrics( font ).tightBoundingRect( the longst string );
              w->setFixedWidth( fontBoundRect.width() );
              sure you may need some margins.

              idlefrogI Offline
              idlefrogI Offline
              idlefrog
              wrote on last edited by idlefrog
              #9

              @JoeCFD said in Lost on resizing QListWidget :(:

              QFont font = w->font(); /*get default font */
              QRectF fontBoundRect = QFontMetrics( font ).tightBoundingRect( the longst string );
              w->setFixedWidth( fontBoundRect.width() );

              That works in the same way as my bruit force method, although I prefer to use:
              auto column_width = w.sizeHintForColumn(0); as there is only one column.

              For a simple list widget I would expect to see some 'fitToContents' behaviour.
              Perhaps it is hidden somewhere, but I haven't uncovered it yet.

              (Manually adjusting the size by calculating the contents/margins/frame every time I add/remove an item seems complicated for something that simple).

              JoeCFDJ 1 Reply Last reply
              0
              • idlefrogI idlefrog

                @JoeCFD said in Lost on resizing QListWidget :(:

                QFont font = w->font(); /*get default font */
                QRectF fontBoundRect = QFontMetrics( font ).tightBoundingRect( the longst string );
                w->setFixedWidth( fontBoundRect.width() );

                That works in the same way as my bruit force method, although I prefer to use:
                auto column_width = w.sizeHintForColumn(0); as there is only one column.

                For a simple list widget I would expect to see some 'fitToContents' behaviour.
                Perhaps it is hidden somewhere, but I haven't uncovered it yet.

                (Manually adjusting the size by calculating the contents/margins/frame every time I add/remove an item seems complicated for something that simple).

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #10

                @idlefrog Some basic operation. Better to know it. This applies for more scenarios. Often, some features do not exist while they are expected from Qt.

                idlefrogI 1 Reply Last reply
                0
                • idlefrogI Offline
                  idlefrogI Offline
                  idlefrog
                  wrote on last edited by idlefrog
                  #11

                  This is my final solution for anyone else out there wondering why their list will not 'fit to content':

                  #include <QApplication>
                  #include <QListWidget>
                  #include <QDebug>
                  #include "listwidget.h"
                  
                  class ListWidget : public QListWidget
                  {
                  public:
                      explicit ListWidget(QWidget *parent = nullptr):QListWidget(parent){}
                      ~ListWidget(){}
                  
                      QSize sizeHint() const
                      {
                          QSize size = QListWidget::sizeHint();
                          auto column_width = sizeHintForColumn(0);
                          auto frame_width = frameWidth();
                          auto margins = contentsMargins();
                          auto new_width = column_width+frame_width + margins.left()+margins.right();
                          size.setWidth(new_width);
                  
                          return size;
                      }
                  };
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      ListWidget w;
                      QStringList items = {"C2H6O - Ethanol",
                                           "CHCl - Trichloromethane",
                                           "CO2 - Carbon Dioxide",
                                           "H2O - Water",
                                           "NaCl - Sodium Choride",
                                           "NH3 - Ammonia"  };
                      w.addItems(items);
                      w.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
                      w.show();
                      return a.exec();
                  }
                  

                  Which looks like this:
                  6182b3f5-70e8-4d71-9bbf-3c9b760e4094-image.png

                  JonBJ 1 Reply Last reply
                  2
                  • JoeCFDJ JoeCFD

                    @idlefrog Some basic operation. Better to know it. This applies for more scenarios. Often, some features do not exist while they are expected from Qt.

                    idlefrogI Offline
                    idlefrogI Offline
                    idlefrog
                    wrote on last edited by
                    #12

                    @JoeCFD Thanks!... It's surprising really. Perhaps Widgets are not being developed now.

                    1 Reply Last reply
                    0
                    • idlefrogI idlefrog

                      This is my final solution for anyone else out there wondering why their list will not 'fit to content':

                      #include <QApplication>
                      #include <QListWidget>
                      #include <QDebug>
                      #include "listwidget.h"
                      
                      class ListWidget : public QListWidget
                      {
                      public:
                          explicit ListWidget(QWidget *parent = nullptr):QListWidget(parent){}
                          ~ListWidget(){}
                      
                          QSize sizeHint() const
                          {
                              QSize size = QListWidget::sizeHint();
                              auto column_width = sizeHintForColumn(0);
                              auto frame_width = frameWidth();
                              auto margins = contentsMargins();
                              auto new_width = column_width+frame_width + margins.left()+margins.right();
                              size.setWidth(new_width);
                      
                              return size;
                          }
                      };
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          ListWidget w;
                          QStringList items = {"C2H6O - Ethanol",
                                               "CHCl - Trichloromethane",
                                               "CO2 - Carbon Dioxide",
                                               "H2O - Water",
                                               "NaCl - Sodium Choride",
                                               "NH3 - Ammonia"  };
                          w.addItems(items);
                          w.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
                          w.show();
                          return a.exec();
                      }
                      

                      Which looks like this:
                      6182b3f5-70e8-4d71-9bbf-3c9b760e4094-image.png

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #13

                      @idlefrog
                      Your solution uses sizeHintForColumn() and overrides sizeHint(). I believe this is indeed the correct approach, as per the accepted solution at QListWidget adjust size to content. However a couple of comments there claim not working at Qt 5.x, so good if it works for you.

                      idlefrogI 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @idlefrog
                        Your solution uses sizeHintForColumn() and overrides sizeHint(). I believe this is indeed the correct approach, as per the accepted solution at QListWidget adjust size to content. However a couple of comments there claim not working at Qt 5.x, so good if it works for you.

                        idlefrogI Offline
                        idlefrogI Offline
                        idlefrog
                        wrote on last edited by
                        #14

                        @JonB Yes, it works fine for me on Qt 5.15.2. Thanks for the support!
                        And thanks for the link! (I had found that one too!). :)

                        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