[Translate]-6-1 arrange the controls on the form (laying out Widgets on a form)

Source: Internet
Author: User
Tags qt designer

arranging controls on the form (laying out Widgets on a form)In Chinese and English comparison:
form (form), layout (arrangement or arrangement, meaning the process of arranging controls on a form, such as size, position, etc.) absolute positioning (absolute position positioning), Manual layout (manual), layout There are three ways to manage the layout of controls on a form in managers (layout manager) QT: Absolute position positioning (absolute positioning), manual layout (manual layout), layouts managers. We use the dialog box in Figure 6.1 as an example to illustrate each of these three ways. Figure 6.1. The Find File dialog

The absolute positioning method is the most primitive way to arrange the controls. This method is to call the control's function in the program to set its position and size relative to the form. The following is a findfiledialog constructor that is implemented using a method. Findfiledialog::findfiledialog (Qwidget *parent)    : Qdialog (parent) {    ...     Namedlabel->setgeometry (9, 9,;   ) Namedlineedit->setgeometry (65, 9, 200, 25) ;    Lookinlabel->setgeometry (9, (+),;    lookinlineedit->setgeometry (65 , Max, (+);    subfolderscheckbox->setgeometry (9,;   , Max.) tablewidget- >setgeometry (9, Max,;    messagelabel->setgeometry (9, 206, ());    Findbutton->setgeometry (271, 9, GB);    stopbutton->setgeometry (271, +,-a);     closebutton->setgeometry (271, 271, 85,;    helpbutton->setgeometry (199), ;    setwindowtitle (tr ("Find Files or Folders"));    setfixedsize (365, 240);} This method has many drawbacks:1.          users cannot change the size of the form 2.           If you change the font or translate to another language, the text on the control may not be fully displayed 3.           In some styles, the size of the control will not be appropriate another method is manual layout. gives the absolute position of the control, but their size is determined by the size of the window, which can be set by overriding the form's resizeevent () to the size of the child control: Findfiledialog::findfiledialog (Qwidget *parent)    : Qdialog (parent) {    ...    setminimumsize (265);     Resize (365, 240);} void Findfiledialog::resizeevent (qresizeevent */* Event */) {    int extrawidth = width ()-Minimumwidth () ;    int extraheight = height ()-minimumheight ();    namedlabel->setgeometry (9, 9, 50 , (+);    Namedlineedit->setgeometry (9, + extrawidth, +);    lookInLabel- >setgeometry (9, (+), (+);    lookinlineedit->setgeometry (n/A, (+), + extrawidth);  & nbsp;  Subfolderscheckbox->setgeometry (9, 156 + extrawidth, max);    Tablewidget->setgeometry (9, +, 156 + extraWidth,& nbsp;                             + extraheight);    messagelabel- >setgeometry (9, 156 + extraheight, 156 + extrawidth,                               ;    findbutton->setgeometry (171 + extrawidth, 9, (+),;    stopButton-) >setgeometry (171 + Extrawidth, 84, 85, 32);    closebutton->setgeometry (171 + extraWidth );    helpbutton->setgeometry (171 + extrawidth, 149 + extraheight, 85,                             32);} In the Findfiledialog constructor, the minimum size of the set form is 265x190 and the initial size is 365x240. In Resizeevent (), the variable extrawidth and extraheight are the difference between the control's relative minimum size and the size of the child control based on the difference, which changes the size of the control when the form is resized. Figure 6.2. Resizing a resizable dialog    absolute location and manual layout management both require more code and require more constant participation in the calculation. Writing the code is annoying, and if the design changes, all the values are recalculated again. Although the manual layout can change the size of the space, but sometimes will not be able to display all the text, in order to avoid this error, you can consider the sizehint of the control, but this code is more complex. The simplest way to manage controls on a form is to use the QT layout management class. These classes can give default values for all types of controls, and can be sizehint for different controls depending on the font, style, and content of the control. The layout management class is able to get the maximum and minimum size of the control, automatically adjusting the layout when the font, content, or window changes. Qhboxlayout,qvboxlayout,qgridlayout is the three most important layout managers that inherit from Qlayout and Qlayout provide the most basic framework for layout. These three classes can be used in code or in QT Designer, following the code findfiledialog::findfiledialog (Qwidget *parent) that Findfiledialog uses the layout manager     : Qdialog (parent) {    ...    qgridlayout *leftlayout = new Qgridlayout;     leftlayout->addwidget (namedlabel, 0, 0);    Leftlayout->addwidget ( Namedlineedit, 0, 1);    leftlayout->addwidget (Lookinlabel, 1, 0);    leftLayout- >addwidget (Lookinlineedit, 1, 1);    Leftlayout->addwidget (Subfolderscheckbox, 2, 0, 1, 2);     Leftlayout->addwidget (tablewidget, 3, 0, 1, 2);    LeftlayOut->addwidget (Messagelabel, 4, 0, 1, 2);    qvboxlayout *rightlayout = new qvboxlayout;  & nbsp Rightlayout->addwidget (Findbutton);    rightlayout->addwidget (StopButton);     Rightlayout->addwidget (CloseButton);    rightlayout->addstretch ();    Rightlayout->addwidget (HelpButton);    qhboxlayout *mainlayout = new QHBoxLayout;     Mainlayout->addlayout (leftlayout);    mainlayout->addlayout (rightLayout);     setlayout (mainlayout);    setwindowtitle (tr ("Find Files or Folders");} Qhboxlayout,qgridlayout and Qvboxlayout are used in the code. The child control on the left side of the form is owned by Qgridlayout, and the child control on the right is owned by Qvboxlayout. These two layouts are controlled by the qhboxlayout. The edge size around the dialog box and the interval between controls are set to the default values for the current spatial style, and the functions Qlayout::setmargin () and qlayout::setspacing () can modify both values. This dialog can also be implemented using QT Designer, first placing all the child controls in approximately the appropriate location, selecting the controls that need to be managed together with the Layout manager, click form| Layout horizontally,form| Layout vertically or form| Layout in a Grid. In the second chapter weThe spreadsheet program's Go-to-cell dialog box and Sort dialog box are created. Figure 6.3. The Find File dialog ' s layout

The use of Qhboxlayout and Qvboxlayout is simple and qgridlayout is a bit complicated. Qgridlayout work is based on a two-dimensional cell. The upper-left corner of the Qlabel position in the layout is (0,0), and the Qlineedit position next to (0,1). Qcheckbox occupies a space of (2,0) and (2,1) two columns, as is the case with Qtreewidget and Qlabel below. The Qgridlayout::addwidget () syntax is as follows: Layout->addwidget (widget, row, column, RowSpan, ColumnSpan), the parameter widget is a child control inserted into this layout, (Row,column) The upper-left cell position occupied by the control, RowSpan is the number of rows that the control occupies, and Colunmspan is the number of columns that the control occupies. The default value for RowSpan and Colunmspan is 1. The function Addstretch () causes the layout manager to leave a space at the specified location. In the above code, the layout manager leaves an extra gap between the close button and the Help button. In QT Designer, we can add a spacer to implement this function, in QT Designer, the spacer is shown as a blue spring-style polyline. There are many other features that you can use with the layout management class. If you add a control to a layout or remove a control from the layout, the layout manager automatically adapts to the change and resizes the control. The layout manager also adjusts automatically when you call the Hide () or show () function of a child control. If the sizehint of the child control changes, the layout manager adjusts to the new sizehint of the control. The layout manager also calculates the minimum size of the entire form, based on the minimum size and sizehint of all child controls. In the example above, we just put the controls in the layout and use spacer (stretches) to fill the rest of the space. Sometimes, these are not enough, we can also change the sizepolicy of the control, or sizehint, so that the layout of the form more in line with our needs. The Sizepolicy of a control describes how the control is scaled in layout management. The controls provided by QT have a reasonable default sizepolicy, but this default value sometimes does not fit all layouts, and developers often need to change the sizepolicy of some controls on the form. All variables of a qsizepolicy apply to both horizontal and vertical directions. Some of the longest values are listed below: 1.     &nbsp   fixed: The control cannot be zoomed in or out, and the size of the control is its sizehint. 2.         minimum: The sizehint of the control is the minimum size of the control. The control cannot be less than this sizehint, but can be enlarged. 3.         maximum: The control's sizehint is the maximum size of the control, and the control cannot be zoomed in, but it can be scaled down to its minimum allowable size. 4.         preferred: The sizehint of the control is its sizehint, but it can be scaled up or down 5.          expandint: Control can increase or decrease itself figure 6.4 with text "Some text" Qlabel shows the meaning of these different sizepolicy, Figure 6.4. The meaning of the different size policies  In the picture, preferred and expanding are the same, what is the difference between them? If a form has both a preferred control and a expanding control, the expanding control fills the rest of the control when it changes size, and the preferred control does not change to consider it sizehint. There are also two sizepolicy values of minimumexpanding and ignored. Minimumexpanding is sometimes used in the old QT version, but it's no longer necessary. Replace the method with the expanding value and override the appropriate Minimumsizehint () function. Ignored and expanding are similar, except that it ignores the sizehint of the control and the smallest sizehint. In addition to the horizontal and vertical values, the Qsizepolicy also contains a horizontal and vertical scaling multiplier (stretch factor). When the form is zoomed in, these two values determine how much different controls zoom in. For example, if Qtreewidget and Qtextedit are arranged up and down, if we want the qtextedit height to be twice times the Qtreewidget, you can set the vertical indent of Qtextedit to 2,qtreewidget with a vertical scaling factor of 1. The minimum size, maximum size, and fixed size of the control are also factors that affect the layout. These restrictions are considered when the layout manager arranges controls. If these are not enough, you can create a new class to override Sizehint ().

[Translate]-6-1 arrange the controls on the form (laying out Widgets on a form)

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.