Layout manager integrated instance and layout manager instance
1. Requirement Analysis
1.1,Develop an exerciseWizard user interface
1.1.1,InSame interfaceDisplay onDifferent wizard pages
1.1.2,Pass"Previous" and "Next"Button to switch
1.1.3,On different pagesElement components and element LayoutAre not the same
1.1.4,Components on the pageArrange the layout through the layout manager
2. Solutions
2.1,Interface Design through layout nesting
2.2,Manage different pages through QStackedLayout
2.3,Use child components to generate different methods
2.4,Notes
2.4.1,ArbitraryContainer ComponentsBothYou can specify the layout manager.
2.4.2,In the same layout managerComponent ownershipSame parent component !!!
2.4.3,When layout management is configuredImplicitly specifiedParent-child relationship
. H header file
# Ifndef WIDGET_H
# Define WIDGET_H
# Include
# Include
# Include
# Include
# Include
Class Widget: public QWidget
{
Q_OBJECT
Private:
QLabel label1;
QLabel label2;
QLabel label3;
QLabel label4;
QLineEdit edit; // If the initialization list order is different from the Declaration Order, a warning is reported,
QPushButton preBtn;
QPushButton nextBtn;
QStackedLayout sLayout; // It is defined here for the convenience of Slot functions. It was originally applied for partial new in the heap space.
Void initControl ();
QWidget * pageone (); // three pages.
QWidget * pagetwo ();
QWidget * pagethree ();
Private slots:
Void onPreBtnClicked (); // slot function, message processing function.
Void onNextBtnClicked ();
Public:
Widget (QWidget * parent = 0 );
~ Widget ();
};
# Endif // WIDGET_H
. Cpp source file
# Include "widget. h"
# Include
# Include
# Include
# Include
# Include
# Include
Widget: Widget (QWidget * parent) // parent component to manage child widgets.
: QWidget (parent), preBtn (this), nextBtn (this), edit (this), label1 (this), label2 (this), label3 (this), label4 (this)
{
InitControl ();
}
Void Widget: initControl ()
{
QVBoxLayout * vLayout = new QVBoxLayout (); // 1. defines the horizontal, vertical, and STACK layout manager of the main interface.
QHBoxLayout * hLayout = new QHBoxLayout ();
PreBtn. setText ("Pre Page ");
PreBtn. setSizePolicy (QSizePolicy: Expanding, QSizePolicy: Fixed); // The first parameter is horizontal and the second parameter is vertical.
PreBtn. setMinimumSize (160, 30 );
NextBtn. setText ("Next Page ");
NextBtn. setMinimumSize (160, 30 );
NextBtn. setSizePolicy (QSizePolicy: Expanding, QSizePolicy: Fixed );
// 2. Add the two buttons to the horizontal layout.
HLayout-> addWidget (& preBtn );
HLayout-> addWidget (& nextBtn );
// 3. Add the implementation page to the stack Manager
SLayout. addWidget (pageone ());
SLayout. addWidget (pagetwo ());
SLayout. addWidget (pagethree ());
// 4. yunshui and stack manager to the vertical Manager
VLayout-> addLayout (& sLayout );
VLayout-> addLayout (hLayout );
// 5. Set the overall layout manager
This-> setLayout (vLayout );
// 6 connection signals and slots.
Connect (& preBtn, SIGNAL (clicked (), this, SLOT (onPreBtnClicked ()));
Connect (& nextBtn, SIGNAL (clicked (), this, SLOT (onNextBtnClicked ()));
}
QWidget * Widget: pageone () // three pages.
{
QWidget * ret = new QWidget ();
QGridLayout * gLayout = new QGridLayout ();
Label1.setText ("one ");
Label2.setText ("two ");
Label3.setText ("three ");
Label4.setText ("four ");
GLayout-> addWidget (& label1, 0, 0); // the label is in the 0 rows and 0 columns,
GLayout-> addWidget (& label2, 0, 1); // This label is in the 0 rows and 1 column,
GLayout-> addWidget (& label3, 1, 0 );
GLayout-> addWidget (& label4, 1, 1 );
Ret-> setLayout (gLayout); // This widget is managed by the GridLayout layout manager.
// It indicates that the components in the same layout manager have the same parent component. The following is the test code.
QDebug () <"ret =" <ret;
QDebug () <label1.parent ();
QDebug () <label2.parent ();
QDebug () <label3.parent ();
QDebug () <label4.parent ();
Return ret;
}
QWidget * Widget: pagetwo ()
{
QWidget * ret = new QWidget ();
QFormLayout * flayout = new QFormLayout (); // form layout.
Flayout-> addRow ("Name:", & edit); // Add the Tag Name and Component
Ret-> setLayout (flayout); // you can specify the layout manager widget.
Return ret;
}
QWidget * Widget: pagethree () // if you have a new item, you need to find a father for it.
{
QWidget * ret = new QWidget ();
QPushButton * btn1 = new QPushButton ("this is ");
QPushButton * btn2 = new QPushButton ("a page ");
QVBoxLayout * vlayout = new QVBoxLayout ();
Vlayout-> addWidget (btn1 );
Vlayout-> addWidget (btn2 );
Ret-> setLayout (vlayout );
Return ret;
}
Void Widget: onPreBtnClicked () // slot function, message processing function.
{
// Here is a small trick, that is,-1 will change to a negative number; solution: + total number and then Remainder
Int index = (sLayout. currentIndex ()-1) + sLayout. count () % sLayout. count ();
SLayout. setCurrentIndex (index );
}
Void Widget: onNextBtnClicked ()
{// Cyclically obtain the remainder to prevent overflow
Int index = (sLayout. currentIndex () + 1) % sLayout. count ();
SLayout. setCurrentIndex (index );
}
Widget ::~ Widget ()
{
}
Main. cpp File
# Include
# Include "widget. h"
Int main (int argc, char * argv [])
{
QApplication a (argc, argv );
Widget w;
W. show ();
Return a.exe c ();
}
3. Summary
3.1,Layout managerYesMutual nestingComplex user interfaces
3.2,ArbitraryContainer ComponentsAverageConfigurable layout manager
3.3,Same layout managerComponents inHaving the same parent component
3.4,Parent-child relationship between componentsYes in QtImportant methods of Memory Management