Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-113014

FlowLayout example has wrong size hint

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.5.0
    • Widgets: Layout
    • None
    • All

    Description

      The example FlowLayout (an example of custom layout which is bundled among official project examples in Qt framework) implements sizeHint() as

      QSize FlowLayout::sizeHint() const
      {
          return minimumSize();
      }
      

      This however does not make much sense, IMO, and in some complicated combinations of parent layouts and size constraints it leads to unwanted effects and bad layout calculations (I did not however managed to create a minimum reproducible example to document it - the layout situations were too complex with too many layers of layouts and splitters to be called "minimum"). I believe that the proper size hint should be calculated from the child items laid out horizontally next to each other. Possible solution:

      QSize FlowLayout::sizeHint() const
      {
           // TODO: the algorithm should take into account the child visibility too
          QMargins margins = contentsMargins();
          int h = margins.top() + margins.bottom();
          int w = margins.left() + margins.right() + horizontalSpacing() * ( m_itemList.size() - 1 );
      
          for( const QLayoutItem * item : qAsConst( m_itemList ) )
          {
              QSize s = item->sizeHint();
              h = qMax( h, s.height() );
              w += s.width();
          }
      
          return QSize( w, h );
      }
      

      This solution seems to fix the problems which I encountered in the more complex parent layouts...

      Btw. I really believe that FlowLayout should really go from examples to official QFlowLayout so that it is easier to discover and usable also in QtDesigner. Flow layouts are a basis for adaptive layouts. There is no alternative. But there are already tickets for this change.

      PS: there is also FlowLayout present in QtCreator code base, it has the same implementation of sizeHint() as in examples. It should probably be fixed too.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            vladimir.kraus Vladimir Kraus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes