13

Can anyone give a clear explain of these 3 concept? What's the difference and how to use them?

Alston
  • 1,166
  • 2
  • 13
  • 26

2 Answers2

20

size hint is the preferred size of the widget, layouts will try to keep it as close to this as possible.

size policy describes how the size may change when the preferred size cannot be used (can it stretch or shrink) see the QSizePolicy::Policy enum for a description of each.

size constraint are the maximumSize and minimumSize the widget can be.

Yuval Langer
  • 649
  • 1
  • 5
  • 16
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • 1
    By this explanation, what would be the relationship between say `maximumSize` and `maximumSizeHint`? – user35443 Jun 06 '17 at 16:38
  • @user35443 I believe `maximumSize` and `maximumSizeHint` refer to a size constraint and a size hint, respectively. – Sam Gomena Apr 04 '18 at 06:00
  • 1
    maximumSize and minimumSize can be set by the *user* of the widget. minimumSizeHint can be set by the *implementation* of the widget. I don't think maximumSizeHint exists in qt5. – textshell Jun 05 '21 at 20:37
  • @textshell But the minimum hint still exists, and your line answers it all for me, thank you! – vines Sep 28 '22 at 09:56
0

It puzzles me a lot too until I understand the Layout Management of Qt. According to its docs:

When you add widgets to a layout, the layout process works as follows:

  1. All the widgets will initially be allocated an amount of space in accordance with their QWidget::sizePolicy() and QWidget::sizeHint().

  2. If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor (explained below).

  3. If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space. Of these, space is allocated to widgets with an Expanding size policy first.

  4. Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require. (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.)

  5. Any widgets that are allocated more space than their maximum size are allocated the maximum size space they require. (Widgets do not have to have a maximum size in which case the stretch factor is their determining factor.)

And sizeHint() is the recommended size of a QWidget, and the Layout of the widget parent will take sizeHint() and sizePolicy() into consideration to determine the space of the child widget can hold.

Community
  • 1
  • 1
Charles Sun
  • 550
  • 4
  • 7