1.4 widget layout, 1.4 widget Layout

Source: Internet
Author: User

1.4 widget layout, 1.4 widget Layout

The last part in chapter 1 of Qt is the layout of components.

The example in the book uses a QHBoxLayout class.

This class can automatically adjust the size and position of controls placed in the layout. We do not need to adjust them manually, which is more convenient.

Row 3: Create a QWidget object

QWidget is the base class of all user interface objects. It will be used as the parent object of other parts, and the corresponding control is displayed above. That is, it serves as the main window of the program.

Row 11th: Set the window title

Literally, the title of the window is also set.

Row 3: Create a QSpinBox object

Functions used:QSpinBox ::QSpinBox(QWidget*Parent= 0)

Function Description: Create a fine-tuning box. The default value is 0, the maximum value is 99, and the initial value is 0.

Row 3: Create a QSlider object

Functions used:QSlider ::QSlider(Qt: OrientationOrientation,QWidget*Parent= 0)

Function Description: Construct a slider and specify the slider direction. Qt: Vertical is the Vertical direction, and Qt: Horizontal is the Horizontal direction.

15th ~ Row 16: Set the valid range.

Function used by QSpinBox: voidQSpinBox ::SetRange(IntMinimum,IntMaximum)

Function used by QSlider: voidQiniactslider ::SetRange(IntMin,IntMax)

Function Description: Set the valid range of the fine-tuning box and slider. After the setting is complete, the range will not be exceeded in actual use, if you manually enter an invalid value in the fine-tuning box, it will not be input normally, which ensures the validity of the value.

18th ~ Line 21: Set the connection between the signal and the slot

Both QSpinBox and QSlider have a valueChanged (int) signal, indicating that valueChanged is triggered when the value changes.

They also have a setValue (int) slot.

After the valueChanged signal of QSpinBox is connected to the setValue slot of QSlider, the value of QSlider also changes when the value of QSpinBox changes.

Similarly, we can understand what will happen to another connection.

In addition, there may be a question: the value of the fine-tuning box is changed, and it will call the setValue of the slider to set the value of the slider, so that the value of the slider also changes, the slider also calls the setValue of the fine-tuning box to set the fine-tuning box ...... It seems like such an endless loop ...?

I don't know if it's my relationship as a beginner. I don't know if other people will? When I learned this, I had this question. Later, I explained in my book that there will not be such an endless loop.

Therefore, you need to know when the object will send a signal. It is conditional. The condition for the fine-tuning box to transmit the valueChanged signal is:When the value has changed.

If the value changes, it emits a signal. The slider responds and calls the setValue slot to change its own value. Then its value "changed ", but the change here is just "replace" with the new value in the fine-tuning box.

If the new value is the same as the existing value of the slider, It is not changed. Therefore, the slider value remains unchanged and does not matchThe value has changed.This condition will not trigger a signal again.

A question is solved in this way, and it will not create an endless loop.

Row 3: Set the QSpinBox value.

Because the signal has been connected to the slot, if you set the value after the connection, the signal will be sent and the slot will be executed accordingly, set the value of QSlider to the same value as that of QSpinBox.

Row 24th: Creates a QHBoxLayout object.

Functions used:QHBoxLayout ::QHBoxLayout()

Function Description: Create a horizontal layout manager to manage the positions and sizes of each control.

25th ~ Row 26: Add a widget to a horizontal layout object.

Function used: voidQBoxLayout ::AddWidget(QWidget*Widget,IntStretch= 0,Qt: AlignmentAlignment= 0)

Function Description: Add a widget to the layout and adjust the size and position of each widget in the layout.

Row 27th: Set the layout manager on the main window.

Function used: voidQWidget ::SetLayout(QLayout*Layout)

Function Description: Set the layout manager to the layout of this part.

 

Finally, show () is the main window.

No matter how you adjust the window width, the fine-tuning box and slider will automatically adjust the size, which is indeed a very convenient thing.

 

The following details the layout manager:

The layout manager is an object that allows you to set the size and position of the window parts in charge of it.

There are three main layout manager classes in Qt:

QHBoxLayout: arranges widgets horizontally from left to right. Header file <QHBoxLayout>.

QVBoxLayout: arrange the widgets in the vertical direction from top to bottom. Header file <QVBoxLayout>.

QGridLayout: arranges various widgets in a grid. Header file <QGridLayout>.

 

QHBoxLayout, horizontal layout manager, is like this:

 

QVBoxLayout, vertical layout manager, is like this:

 

QGridLayout, grid layout manager, is like this:

Function used: voidQGridLayout ::AddWidget(QWidget*Widget,IntRow,IntColumn,Qt: AlignmentAlignment= 0)

Function Description: The widget specifies the object to be added to the layout manager. Row and column are the locations where objects are placed.

QGridLayout adds all parts to the grid, just like a table. You can specify which row and column of the parts are placed in the table. The same is true.

 

 

These three layout managers can be nested to make a nice layout.

In combination with the knowledge of C ++ and the knowledge of Qt I have learned before, let me get a free interface ~

 

# Include <QApplication> # include <QPushButton> # include <QLineEdit> # include <QSlider> # include <QSpinBox> # include <QLabel> # include <QLayout> // contains <QLayout> you can no longer include the following three header files/* # include <QHBoxLayout> # include <QVBoxLayout> # include <QGridLayout> */enum LAYOUT_TYPE {layout_H, // QHBoxLayoutlayout_V, // QVBoxLayoutlayout_G // QGridLayout}; QLayout * getLayout (LAYOUT_TYPE); int main (int argc, char * argv []) {QApp Lication app (argc, argv); QWidget * window = new QWidget; window-> setWindowTitle ("Hello Qt! "); /***************** Set top menu options ****************** /QHBoxLayout * Top = (QHBoxLayout *) getLayout (layout_H); QPushButton * btn_Menu1 = new QPushButton ("Start"); QPushButton * btn_Menu2 = new QPushButton ("Online "); QPushButton * response = new QPushButton ("Setting"); QPushButton * response = new QPushButton ("Exit"); Top-> addWidget (response); Top-> addWidget (btn_Menu2 ); top-> addWidget (btn_Menu3); Top-> addWidg Et (btn_Menu4 ); /**************** set the left-side widget ****************** /QVBoxLayout * midLeft = (QVBoxLayout *) getLayout (layout_V); QHBoxLayout * midLeftLayout1 = (QHBoxLayout *) getLayout (layout_H); QLabel * tip1 = new QLabel ("Sound Volume"); // You can manually set the direction of QSlider, horizontal is Horizontal, Vertical is Vertical QSlider * slider1 = new QSlider (Qt: Horizontal); slider1-> setRange (0,100); slider1-> setValue (50 ); midLeftLayout1-> addWidget (tip1 ); MidLeftLayout1-> addWidget (slider1); QHBoxLayout * midLeftLayout2 = (QHBoxLayout *) getLayout (layout_H); QLabel * tip2 = new QLabel ("Sound Effect "); QSlider * slider2 = new QSlider (Qt: Horizontal); slider2-> setRange (0,100); slider2-> setValue (50); midLeftLayout2-> addWidget (tip2 ); midLeftLayout2-> addWidget (slider2); midLeft-> addLayout (midLeftLayout1); midLeft-> addLayout (midLeftLayout2 ); /****************** Set the central right part to *****************/QVBoxLayout * midRight = (QVBoxLayout *) getLayout (layout_V ); QHBoxLayout * midRightLayout1 = (QHBoxLayout *) getLayout (layout_H); QLabel * tip3 = new QLabel ("Game Levels"); QSpinBox * spinBox = new QSpinBox (); spinBox-> setRange (1, 10); spinBox-> setValue (1); midRightLayout1-> addWidget (tip3); midRightLayout1-> addWidget (spinBox ); QHBoxLayout * midRightLayout2 = (QHBoxLayout *) getLay Out (layout_H); QLabel * tip4 = new QLabel ("Player Name"); QLineEdit * Edit = new QLineEdit (); midRightLayout2-> addWidget (tip4 ); midRightLayout2-> addWidget (Edit); midRight-> addLayout (midRightLayout1); midRight-> addLayout (midRightLayout2 ); // Add the layout manager of the left and right sides of the central area to QHBoxLayout * Mid = (QHBoxLayout *) getLayout (layout_H); Mid-> addLayout (midLeft ); mid-> addLayout (midRight ); /**************** set the bottom part ************ * *****/QHBoxLayout * Bottom = (QHBoxLayout *) getLayout (layout_H); QLabel * lab = new QLabel ("All right reserved. "); // addStretch () is used to add delimiters for placeholder. // if the size of an existing part is determined to be the best, // Add a separator to occupy unnecessary spaces, so that parts cannot be adjusted to occupy all spaces. // Add addStretch () before and after the lab to center the lab ~ Bottom-> addStretch (); Bottom-> addWidget (lab); Bottom-> addStretch (); /***************** set the main layout manager ****************** /QVBoxLayout * mainLayout = (QVBoxLayout *) getLayout (layout_V); mainLayout-> addLayout (Top); mainLayout-> addLayout (Mid); mainLayout-> addLayout (Bottom); window-> setLayout (mainLayout ); window-> show (); return app.exe c ();} QLayout * getLayout (LAYOUT_TYPE type) {QLayout * Layout; switch (type) {case layout_H: Layout = new QHBoxLayout; break; case layout_V: Layout = new QVBoxLayout; break; case layout_G: Layout = new QGridLayout; break;} return Layout ;}

 

 

Okay. Here we will summarize it ~

 

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.