17

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

2 Answers 2

26

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.

4
  • 1
    By this explanation, what would be the relationship between say maximumSize and maximumSizeHint?
    – user35443
    Commented Jun 6, 2017 at 16:38
  • @user35443 I believe maximumSize and maximumSizeHint refer to a size constraint and a size hint, respectively.
    – Sam Gomena
    Commented Apr 4, 2018 at 6: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
    Commented Jun 5, 2021 at 20:37
  • @textshell But the minimum hint still exists, and your line answers it all for me, thank you!
    – vines
    Commented Sep 28, 2022 at 9:56
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.