Size Policy for Qt Quick Controls

When using Qt Quick Layouts, it is necessary for the user to specify the attached properties Layout.fillWidth or Layout.fillHeight to stretch a child component. This process can become cumbersome as more components require it. Inline components may serve as an alternative, but they may not be as effective when dealing with different component types. To tackle these challenges, the size policy feature has been introduced in Qt Quick Controls in version 6.7 (under tech preview), mirroring similar functionality found in widgets.

What is the Size policy? 

The size policy describes the horizontal and vertical resizing policy the item prefers when laid out within the layout. This affects the item size in terms of grow, shrink, or expand. Qt Widgets already supports this feature, which allows each widget type to communicate with the layouts on how they should be resized. This results in the application developer seldom having to specify the resizing behavior of a widget.

For instance, QLineEdit and QSlider have a default horizontal size policy of QSizePolicy::Expanding, so they will be configured to grow by default, without any extra configuration from the application developer.

Qt Quick Layouts specifies size policies in a bit more primitive way: All items in a Qt Quick Layout are by default fixed size, and the only way to specify if a layout should resize an Item is by explicitly setting  - Layout.fillWidth: true or Layout.fillHeight: true as the attached properties on the item. Note that this allows the item to both shrink and grow beyond its preferred size. 

This can lead to some repetitive boilerplate code for common UI components such as TextFields or Sliders where you very often want them to stretch horizontally, but not vertically, so you often end up setting Layout.fillWidth:true each time you declare those items.

Extending Qt Quick Layouts with Size Policies 

To avoid this boilerplate code, we thought it might be a good idea to introduce the same QWidget size policy mechanism to Quick Items, but due to compatibility reasons, the application developer has to enable it explicitly. By enabling this feature, the fixed size assumption by Qt Quick Layouts will be overridden with type specific size policies - so e.g. Button will have a size policy which is preferred horizontally and fixed vertically. 

For now, this feature can be enabled in Qt Quick by setting the global application attribute Qt::AA_QtQuickUseDefaultSizePolicy,


QGuiApplication::setAttribute(Qt::AA_QtQuickUseDefaultSizePolicy);

How will it be useful for the user? 

The users can now avoid setting the attached properties Layout.fillWidth / Layout.fillHeight each time they add a quick item in the layout. Qt Widgets users can migrate to Qt Quick with the expectation that the default layout policy is set for the items within the layout.

In the example provided below, the Layout attached property can be removed if the default size policy is enabled in the application.


RowLayout {
    Label {
        text: "Name"
    }
    TextField {
        placeholderText: "Surname, firstname"
        Layout.fillWidth: true   // <-- Can be removed if default size policy is enabled
    }
}

Current Pitfalls:

  • The solution of using a global switch has a drawback when external components are used in the application (such as components from a third-party library unaware of this feature). This is under discussion, and it will be fixed.

  • The size policy of quick items is currently internal and cannot be explicitly set, but it is still possible to adjust with existing layout attached properties (using Layout.fillWidth and Layout.fillHeight) 

  • The quick item in 6.7.0 does not support the Expanding or Minimum policy, but it shall be supported in the future version. 

 Extend for the future: 

  • The size policy property can be exposed, and the user can set or override default policies for the quick items placed within the layout.

Since this feature is under tech preview, we would like users to experiment this feature and provide their feedback. 


Blog Topics:

Comments