Briefly
Qgridlayout: Grid Layout, also known as Grid layout (multi-row multi-column).
The grid layout places the widgets in it into a mesh grid. Qgridlayout needs to divide the space provided to it into rows and columns, and insert and manage each widget into the correct cell. The grid layout works like this:
It calculates the space in which it is, and then logically divides them into rows and columns (column) and places each widget that is managed by it in the appropriate unit, where the cell (cell) refers to the space divided by the intersection of rows and columns.
In a grid layout, rows and columns are essentially the same, but they are called different. The column is highlighted below, which of course applies to rows as well.
In a raster layout, each column (and row) has a minimum width (using the setcolumnminimumwidth () setting) and a scaling factor (using the Setcolumnstretch () setting). The minimum width refers to the smallest width of the widget in the column, and the scaling factor determines how much space the widget within that column can get.
- Briefly
- Detailed description
- Use
- Common interfaces
- Summarize
Detailed description
In general, we put a widget into a cell in the grid layout, but sometimes the widgets may need to occupy more than one unit. You need to use an overloaded version of the AddWidget () method, with the following prototype:
- void AddWidget (Qwidget *, int row, int column, int rowSpan, int columnspan, qt::alignment = 0);
This unit expands from row and column to the rows and columns of the specified multiples of rowspan and ColumnSpan. If the value of rowspan or ColumnSpan is-1, the widget expands to the bottom of the layout or to the right edge.
Once the raster layout is created, you can use AddWidget (), AddItem (), and the Addlayout () method to add widgets to it, as well as other layouts.
Use
Let's take a penguin as an example to explain how to use Qgridlayout.
Effect
Source
//Build control avatar, user name, password input box, etc.Qlabel*Pimagelabel= NewQlabel (this); Qlineedit*Puserlineedit= NewQlineedit (this); Qlineedit*Ppasswordlineedit= NewQlineedit (this); Qcheckbox*Premembercheckbox= NewQcheckbox (this); Qcheckbox*Pautologincheckbox= NewQcheckbox (this); Qpushbutton*Ploginbutton= NewQpushbutton (this); Qpushbutton*Pregisterbutton= NewQpushbutton (this); Qpushbutton*Pforgotbutton= NewQpushbutton (this);p Loginbutton -Setfixedheight ( -);p Userlineedit -Setfixedwidth ( $);//Set AvatarQpixmap Pixmap (":/images/logo");p Imagelabel -Setfixedsize ( -, -);p Imagelabel -Setpixmap (pixmap);p Imagelabel -Setscaledcontents (true);//Set textPuserlineedit -SetPlaceholderText (Qstringliteral ("QQ number/Mobile/email"));pP Asswordlineedit -SetPlaceholderText (Qstringliteral ("Password"));pP Asswordlineedit -Setechomode (Qlineedit::P assword);p Remembercheckbox -SetText (Qstringliteral ("Remember Password"));p Autologincheckbox -SetText (Qstringliteral ("Automatic Login"));p Loginbutton -SetText (Qstringliteral ("Login"));p Registerbutton -SetText (Qstringliteral ("Registered account"));p Forgotbutton -SetText (Qstringliteral ("Retrieve password")); Qgridlayout*Playout= NewQgridlayout ();//Avatar No. 0 row, No. 0 column start, occupy 3 rows 1 columnsPlayout -AddWidget (Pimagelabel,0,0,3,1);//Username input box No. 0 row, 1th column start, occupy 1 rows 2 columnsPlayout -AddWidget (Puserlineedit,0,1,1,2);p Layout -AddWidget (Pregisterbutton,0,4);//Password input box 1th row, 1th column start, occupy 1 rows 2 columnsPlayout -AddWidget (Ppasswordlineedit,1,1,1,2);p Layout -AddWidget (Pforgotbutton,1,4);//Remember Password line 2nd, column 1th start, occupy 1 rows 1 columns horizontal left vertical centerPlayout -AddWidget (Premembercheckbox,2,1,1,1Qt:: AlignLeft |Qt:: Alignvcenter);//Automatic login 2nd row, 2nd Column start, occupy 1 rows 1 columns horizontal right vertical centerPlayout -AddWidget (Pautologincheckbox,2,2,1,1Qt:: AlignRight |Qt:: Alignvcenter);//Login button 3rd row, 1th column start, occupy 1 rows 2 columnsPlayout -AddWidget (Ploginbutton,3,1,1,2);//Set Horizontal spacingPlayout -Sethorizontalspacing (Ten);//Set Vertical spacingPlayout -Setverticalspacing (Ten);//Set outer spacingPlayout -Setcontentsmargins (Ten,Ten,Ten,Ten); setlayout (playout);
Common interfaces
- AddWidget (qwidget *, int row, int column, qt::alignment = 0)
- AddWidget (qwidget *, int row, int column, int rowSpan, int columnspan, qt::alignment = 0)
Add a widget to the layout.
This unit expands from row and column to the rows and columns of the specified multiples of rowspan and ColumnSpan. If the value of rowspan or ColumnSpan is-1, the window part expands to the bottom of the layout or to the right edge, and qt::alignment is aligned.
-addlayout (qlayout *, int row, int column, qt::alignment = 0)
-addlayout (qlayout *, int row, int column, int rowSpan, int columnspan, qt::alignment = 0)
Similar to AddWidget, this is the addition of layouts.
- Setrowstretch (int row, int stretch)
- Setcolumnstretch (int column, int stretch)
Set the row/column scaling space
Similar to the Addstretch function of Qboxlayout.
- setspacing (int spacing)
- sethorizontalspacing (int spacing)
- setverticalspacing (int spacing)
Set spacing
Setspacing () can set horizontal and vertical spacing at the same time, and after setting, the horizontal and vertical spacing are the same.
Sethorizontalspacing (), setverticalspacing () can set the horizontal spacing, vertical spacing, respectively.
Setrowminimumheight (int row, int minSize)
Set row min height
Setcolumnminimumwidth (int column, int minSize)
Set Column minimum width
- Setorigincorner (Qt::corner)
Set the original orientation
Similar to the Setdirection function of Qboxlayout.
Summarize
When the interface elements are more complex, you should not hesitate to use the grid layout as much as possible, rather than using a combination of horizontal and vertical layouts or nesting, because in most cases the latter tends to make the "situation" more complex and difficult to control. The grid layout gives the interface designer a greater degree of freedom to arrange the composition of the interface elements, but only with minimal complexity overhead.
When you are designing an interface that is similar to two columns and several rows, it is easier to use a form layout than a grid layout.
Qt Grille layout (qgridlayout)