1.3 control geometric arrangement-laying out Widgets

Source: Internet
Author: User
This section describes how to arrange multiple controls in a window. You can use the signal and slot methods to synchronize controls. The program requires the user to input the age through spin box or slider.
The program uses three controls: qspinbox, qslider, and qwidget. Qwidget is the main window of the program. Qspinbox and qslider are placed in qwidget; they are children of qwidget. In turn, we can also say that qwidget is the parent of qspinbox and qslider. Qwidget does not have a parent because it is the top-level window of the program. In the constructor of qwidget and its subclass, there is a qwidget * parameter to specify their parent control.
The source code is as follows:
1 # include <qapplication>
2 # include <qhboxlayout>
3 # include <qslider>
4 # include <qspinbox>
5 Int main (INT argc, char * argv [])
6 {
7 qapplication app (argc, argv );
8 qwidget * window = new qwidget;
9 window-> setwindowtitle ("Enter your age ");
10 qspinbox * spinbox = new qspinbox;
11 qslider * slider = new qslider (QT: horizontal );
12 spinbox-> setrange (0,130 );
13 slider-> setrange (0,130 );
14 qobject: connect (spinbox, signal (valuechanged (INT )),
15 slider, slot (setvalue (INT )));
16 qobject: connect (slider, signal (valuechanged (INT )),
17 spinbox, slot (setvalue (INT )));
18 spinbox-> setvalue (35 );
19 qhboxlayout * layout = new qhboxlayout;
20 Layout-> addwidget (spinbox );
21 Layout-> addwidget (slider );
22 window-> setlayout (layout );
23 window-> show ();
24 return app.exe C ();
25}
Line 8 and 9 create the main window control of the program and set the title. Rows 10th to 13 create the children of the Main Window and set the allowed value range. Lines 14th to 17th are links between the spinbox and slider to display the same age value synchronously. Regardless of the value of the control, the valuechanged (INT) signal is sent. The setvalue (INT) function of the other control sets a new value for the control.
When the value of the 18th line is set to 35, the spinbox sends the valuechanged (INT) signal and the int parameter value is 35. This parameter is passed to the setvalue (INT) function of the slider, set the slider value to 35. Similarly, Slider sends a valuechanged (INT) signal to trigger the setvalue (INT) function of the spinbox. At this time, because the current value of the Spin box is 35, the spin box will not send any signal and will not cause an endless loop.
In rows 19th to 22, a layout manager is used to arrange the spinbox and slider controls. The layout manager can determine the widget size and position as needed. Qt has three main layout managers:
Qhboxlayout: horizontal layout control.
Qvboxlayout: vertical layout control.
Qgridlayout: controls are arranged in a matrix.
Row 3, qwidget: setlayout (). Place the layout manager on the window. This statement sets the "parent" of the spinbox and slider as the window, that is, the control where the layout manager is located. If a widget is determined by the layout manager, you do not have to specify a clear "parent" control when creating it.
Now, although we haven't seen the size and position of the spinbox and slider controls, they are arranged horizontally. Qhboxlayout can reasonably arrange them. We don't have to worry about the size and location of the controls on the screen in the program. It's just fine to hand it over to the layout manager.
Creating a user interface in QT is simple and flexible. The programmer's task is to instantiate the required controls, set their properties as needed, and put them in the layout manager. The tasks to be completed in the interface are completed by the signal and slot of QT.

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.