qt-Optimized layout structure

Source: Internet
Author: User
Tags qt designer

In each case so far, we have simply placed the widgets in a certain layout. But in some cases, the resulting layout may not seem like the form we want most. In these cases, you can adjust the layout by changing the size policy and the size hints for the widgets you want to place.

1. size hint (size hint) and minimum size hint (minimum size hint)

Before you introduce the size policy for the QT widget, first describe the size hint (sizes hint) and the minimum size hint (minimum size hint).

⑴ Size Tips

The size hint is the size recommended by Qt for a widget. When the Qt GUI widget is initialized, the size hint of the widget is obtained via Qwidget::sizehint (), a virtual function whose prototype is:

Virtual Qsize sizehint () const

In the case where it is not overloaded, the return value is this:

If the widget does not belong to any layout manager, the function returns an invalid value;

If the widget belongs to a layout manager, the function returns a size that the layout manager considers appropriate.

⑵ Minimum size hint

The minimum size hint (minimum size hint) is the minimum size recommended by QT for a widget, and its usage rules are:

If the size of the widget that needs to be drawn (including the length and height of two aspects) is less than its minimum hint (which is often shown in Qt designer as something that is not seen in the compression), and the minimum hint of the widget is within the allowable range of maximum size and minimum size, Then the size of the widget display will be the value of its minimum hint.

The minimum size hint for setting the widget is done by Qwidget::minimumsizehint (). Its return value has the following scenario:

If the widget does not have a layout manager, the function returns an invalid value;

If the widget belongs to a layout manager, the function returns a dimension that the layout manager deems appropriate.

2. Size Policy

The size policy of a widget tells the layout system how to stretch or shrink it. QT provides a reasonable default size policy value for all of its built-in widgets, but because it is not possible to provide a unique default value for each of the possible layouts, it is common for developers to change the size policy of one or two widgets above it in a form. A qsizepolicy contains both a horizontal component and a vertical component. Here are some common values:

Table 11-1 Enumeration Values Qsizepolicy: Contents of the:P olicy

Enumeration constants

Value

Description

Qsizepolicy::fixed

0

The size hint is the only size selection for the widget, so it does not take any scaling.

Qsizepolicy::minimum

Growflag

The size hint is the smallest size of the widget, it doesn't get smaller, but it can get bigger, but the widgets that take the policy don't dominate the "scramble" space.

Qsizepolicy::maximum

Shrinkflag

The size hint is the maximum size of the widget, that is, the widget will not be larger than the size hint. This widget can be freely scaled down without being "required" by other widgets.

Qsizepolicy::P referred

Growflag | Shrinkflag

In general, the widget will take the size hint as its preferred and best choice, but it can also become small enough to be larger, but not dominant. This policy is the default policy for Qwidget widgets.

Qsizepolicy::expanding

Growflag | Shrinkflag | Expandflag

A widget with this strategy can also feel the size hint, but it tends to occupy as much space as possible, and the widget can become small enough.

Qsizepolicy::minimumexpanding

Growflag | Expandflag

The size hint will be the smallest size of the widget, which will occupy as much space as possible. This policy is no longer recommended, it is recommended to replace it with expanding, and overload minimumsizehint ().

Qsizepolicy::ignored

Shrinkflag | Growflag | Ignoreflag

Similar to expanding, except that all the size hints are ignored, and the widget will occupy as much space as possible.

The "Value" column in table 11-1 actually tells us that each strategy is generally "biased", such as the value of qsizepolicy::fixed is 0, then it "tends" to keep its size constant, that is, to keep the size hint. The value of the qsizepolicy::expanding is a 3-value overlay, and the overall "tendency" is to occupy more space, and so on. This provides a basic basis for determining how the patterns of space are used when multiple widgets with different size policies are placed together, and the following are some common combinations.

Widgets of the same size policy are grouped together by the layout manager. In this case, in addition to the window part cannot exceed its size range, the different widgets can scale freely within their allowed range according to their own scaling factor.

Qsizepolicy::fixed is combined with any other size policy.

Widgets with the qsizepolicy::fixed size policy are unchanged in size, that is, the size of the Sizehint (), while the rest of the widgets can be scaled freely within the allowable range.

Qsizepolicy::P referred and qsizepolicy::expanding are combined together.

Widgets with Qsizepolicy::P referred size policy the size of the widget is constant, that is, it considers the size hint to be the best fit, while the other widget sizes can be scaled freely within their allowed range.

Qsizepolicy::ignored and other sizing strategies (except for the qsizepolicy::fixed strategy), different widgets are freely scaled within their allowed range.

Qsizepolicy: When the:P referred,qsizepolicy::minimum and Qsizepolicy::maximum are combined together, each widget can be freely scaled within its permitted range.

3. Scaling factor (stretch factor)

In addition to the horizontal and vertical two components contained in the size policy, the Qsizepolicy class also holds a scaling factor in both horizontal and vertical directions. These scaling factors can be used to describe the different magnification that should be used for different child window parts when the form is enlarged. That is, you need to set the values for Qsizepolicy::horizontalstretch and Qsizepolicy::verticalstretch. By default, the scaling factor for widgets that are grouped together by the layout manager is equal to 0. At this point, the size of the widget is always equal when all the widgets are not larger than the size range allowed.

For example, suppose you have a qtextedit on the right side of a qlistwidget, and you want the qtextedit to be twice times the length of the Qlistwidget, Then you can set the Qtextedit in the horizontal direction of the tensile factor (Qsizepolicy::horizontalstretch) to 2, and the qlistwidget in the horizontal direction of the tensile factor (Qsizepolicy:: Horizontalstretch) is set to 1, and the default is 0 in the vertical direction, which is the same height. This sets the effect of 11-10 as shown.

4. size constraints (size constraint)

Another way to influence layout is to set the maximum size, minimum size, or fixed size of its child widgets. These are done by setting the Sizeconstraint property. The property value is an enumeration constant that defines the pattern of the layout's size constraints. The table lists all of its possible values, and its default value is qlayout::setdefaultconstraint. Get and set the property value can be obtained through qwidget::layout () to get the layout manager of the main window part, and then you can call the Qlayout::sizeconstraint () function to view the current settings, and then pass the Qlayout: The Setsizeconstraint () function to set the Sizeconstraint property of the layout manager. The prototypes of these two functions are as follows:

Sizeconstraint Sizeconstraint () const
sizeconstraint )

Where the value ofSizeconstraint is within the range of the enumeration values in table 11-2.

Table 11-2 Possible values for the size constraint property (qlayout::sizeconstraint) of the layout manager

Constant

Value

Description

Qlayout::setdefaultconstraint

0

The minimum size of the main window part is set to MinimumSize () unless the widget already has a minimum size

Qlayout::setfixedsize

3

The size of the main widget is set to Sizehint (), and the size of the widget is not allowed to change

Qlayout::setminimumsize

2

The minimum size of the main window part is set to MinimumSize (), and the widget cannot be made smaller

Qlayout::setmaximumsize

4

The maximum size of the main window assembly is set to MaximumSize (), and the widget cannot be made larger

Qlayout::setminandmaxsize

5

The minimum size of the main window assembly is set to MinimumSize () and the maximum size is set to MaximumSize ()

Qlayout::setnoconstraint

1

The size of the main window part will not be constrained

5. Blank (margin) and pitch (spacing)

Each layout has two important properties, whitespace, and spacing. Whitespace refers to the distance between the entire layout and the edge of the form, and spacing refers to the distance between the various widgets within the layout manager.

The blank attribute is margin (), the Spacing property is spacing (), and their default values are determined by the style of the form. In the default style of QT, the value of margin () of the subform part is 9 inches, and the margin () value of the form is 11 inches. The value of spacing () is the same as margin ().

If you want to set these two values, you can pass SetMargin () and setspacing ().

Note that starting with Qt4.3, the margin () attribute has gradually ceased to be recommended by QT4, and a better way to set whitespace is to use the Setcontentsmargins () method, which is prototyped as follows:

void qlayout::setcontentsmargins (int left, int top, int. right, int bottom )

Among them, left, top, right, and bottom represent the white space around the layout.

For Qgridlayout and qformlayout, do not use the setspacing () method, but instead use the sethorizontalspacing () and setverticalspacing () methods to set the spacing between the horizontal and vertical directions, respectively. If you use the Setspacing () method and get spacing (), its return value will be-1.

Transferred from: http://blog.csdn.net/qter_wd007/article/details/5377882

qt-Optimized layout structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.