Common methods of Qt layout

Source: Internet
Author: User

The QT layout has the following various

    Qboxlayout              horizontally or vertically arranges controls
    qbuttongroup the            container of the Organization button
    qformlayout             manage the input controls and their associated labels
    Qgraphicsanchor         represents an anchor
    between two items in a qgraphicsanchorlayout Qgraphicsanchorlayout   You can connect anchors to a
    qgridlayout             grid layout (multiple rows and columns) in a graphical view
    qgroupbox               grouped boxes
    with headings Qhboxlayout             Horizontal Arrangement Control
    qlayout                 the base class of the Geometry manager
    qlayoutitem             abstract Operation layout item
    qsizepolicy             Describes policies for horizontal and vertical sizing
    qspaceritem The             space
    qstackedlayout Toggle Control in the layout, with          only one control visible at a time
    qstackedwidget          Toggle control, only one control is visible
    at a time Qvboxlayout             Vertical Arrangement Control
    Qwidgetitem             represents a layout item for a control

Commonly used is the following three kinds of

    Qhboxlayout  Horizontal layout
    qvboxlayout  Vertical layout
    qgridlayout  Grid layout (multiple rows and columns)

The following is a method of centering the cloth to illustrate
(1) Set the layout for the page * This is not the way to layout *
void Qwidget::setlayout (Qlayout * layout)

(2) Adding controls to local
void AddWidget (Qwidget * W)
Another usage: addwidget (button2, 0, Qt::alignleft | Qt::aligntop); You can set the alignment direction

(3) Setting the scaling factor
Setstretch (Int,int)
For example, the layout has a row of three buttons, the layout is set as follows:
Setstretch (0,1)
Setstretch (ON)
Setstretch (2,3)
The three-button width ratio is 1:2:3
But only if you can't control the width of the button in any other way.
One more thing: if the number of controls in the above layout is greater than 3, and the number of controls that only set the scaling factor is less than or equal to 3, there is no effect .... Venture conjecture: The value of this scaling factor should be = the sum of the scaling factor/all control scaling factor for each control

(4) Set the distance between the entire layout and other controls (margin)

SetMargin (int)
setcontentsmargins (int left, int top, int. right, int bottom);

SetMargin can set the left, top, right, and bottom margins, after which their margins are the same.
The setcontentsmargins is the same as its function, but the left, top, right, and bottom margins can be set to a different value.

(5) Set the distance between the control parts of the layout (inner margin)
setspacing (int)

(6) Add expansion space
Addstretch ();
All controls are displayed right before the layout starts adding controls
All controls will be left on display when you have finished adding controls to the layout
All controls on both sides of the layout are centered
All controls on both sides of each control in the layout are evenly displayed

(7) Setting the layout direction
Setdirection (Qboxlayout::toptobottom);
This setting can be a vertical and horizontal interchange direction

(8) Setting the tensile factor
Setstretchfactor (qwidget *w, int stretch);
Setstretchfactor (qlayout *l, int stretch);

If it's a horizontal layout
Setstretchfactor (button1, 1);
Setstretchfactor (Button2, 2);
When the form becomes larger, the button2 is stretched first,
The button1 is then stretched so that the ratio of button1 to button2 width is 1:2.

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.