How does a Qt widget calculate its size?

When I use QWidget to create a user interface, I am always a little confused about its size properties. There size policy , geometry and hintSize .

I just know the connection between size policy and hintSize : size policy based on sizehint . But I do not know other relationships between these properties.

I want to have a widget that can be automatically resized depending on the size of the parent or main window. But I do not know how to use the three properties to achieve this.

+4
source share
1 answer

geometry: The current position and size of the widget inside the parent. For example, a widget with geometry (10, 20, 50, 70) will be located at position (10, 20) inside the parent with a width of 50 and a height of 70.

sizeHint: (perhaps what you are referring to is "hintSize"). The preferred size in which the widget will be. Override this method to change it.

size policy . How the widget will respond when it is placed in a container with dimensions other than its own. Size policies are a combination of:

  • [ QSizePolicy::Maximum ] can a widget shrink below sizeHint
  • [ QSizePolicy::Minimum ] whether the widget can expand over its sizeHint and
  • [ QSizePolicy::Expanding ] whether the widget should expand to fill extra space.

Widgets have both horizontal and vertical size policies.


However, you are probably looking for layouts that automatically resize widgets for your parents based on these rules. For example, for a widget to populate a parent element, set the parent layout to QVBoxLayout , and then add the child widget to the layout. When the parent size is resized, the file size will be resized automatically.

+3
source

Source: https://habr.com/ru/post/1483483/


All Articles