The usual, give a goal to achieve the following:
Code presentation,
First create a project, do not interface, select the base class is Qdialog this, after the name of the words of the random change
My class name is: Stackdlg
So after that, just add two headers to the stackdlg.h and wait for it to be used.
#include <QListWidget> #include <QStackedWidget> #include <QLabel> #include <QHBoxLayout>
All code
#ifndef stackdlg_h#define stackdlg_h#include <QDialog> #include <QListWidget> #include <qstackedwidget > #include <qlabel>class stackdlg:public qdialog{ q_objectpublic: stackdlg (Qwidget *parent = 0); ~stackdlg ();p rivate: qlistwidget *list; list box qstackedwidget *stack; Stack form Qlabel *label1; Text Form Qlabel *label2; Qlabel *label3;}; #endif//Stackdlg_h
And then inside the Stackdlg.cpp is:
#include "stackdlg.h" #include <QListWidget> #include <QStackedWidget> #include <QLabel> #include <qhboxlayout>stackdlg::stackdlg (Qwidget *parent): Qdialog (parent)//inherited constructor {Setwindowtitle (tr (" Stackedwidget ")); Set the name of the form list = new Qlistwidget (this); Create a list box//Add elements to this list box List->insertitem (0, tr ("the")); List->insertitem (1, TR ("No.2")); List->insertitem (2, TR ("No.3")); Label1 = new Qlabel (tr ("Test 1")); Label2 = new Qlabel (tr ("Test 2")); Label3 = new Qlabel (tr ("Test 3")); stack = new Qstackedwidget (this); Create a stack form for this main form stack->addwidget (Label1); Add a text into the form stack->addwidget (Label2); Add a second stack->addwidget (LABEL3); Add a third qhboxlayout *mainlayout = new Qhboxlayout (this); Set a layout mainlayout->setmargin (5); Set the margin for this dialog box mainlayout->setspacing (5); Sets the margin Mainlayout->addwidget (list) between controls; Add the contents of the list to the form inside Mainlayout->addwidget (stack, 0, qt::alignhcenter); Mainlayout->setstretchfactor (list, 3); Set to ScalableControl, the first parameter is the control used to specify the setting, and the second greater than 0 indicates that the control is scalable Mainlayout->setstretchfactor (stack, 3); Connect (list, SIGNAL (currentrowchanged (int)), stack, SLOT (setcurrentindex (int)));} Stackdlg::~stackdlg () {}
The rest has not changed, how feel very tired???
And then modify a little bit, and we'll see what the Qlistwidget really is.
#include "stackdlg.h" #include <QListWidget> #include <QStackedWidget> #include <QLabel> #include <qhboxlayout>stackdlg::stackdlg (Qwidget *parent): Qdialog (parent)//inherited constructor {Setwindowtitle (tr (" Stackedwidget ")); Set the name of the form list = new Qlistwidget (this); Create a list box//Add elements to this list box List->insertitem (0, tr ("the")); List->insertitem (1, TR ("No.2")); List->insertitem (2, TR ("No.3")); List->insertitem (2, TR ("222")); After the addition will cover the front, here and above is not the same, the rest as Label1 = new Qlabel (tr ("Test 1")); Label2 = new Qlabel (tr ("Test 2")); Label3 = new Qlabel (tr ("Test 3")); stack = new Qstackedwidget (this); Create a stack form for this main form stack->addwidget (Label1); Add a text into the form stack->addwidget (Label2); Add a second stack->addwidget (LABEL3); Add a third qhboxlayout *mainlayout = new Qhboxlayout (this); Set a layout mainlayout->setmargin (5); Set the margin for this dialog box mainlayout->setspacing (5); Sets the margin Mainlayout->addwidget (list) between controls; Add the contents of the list to the form mainlayout->addwidget (stack, 0, Qt::aliGnhcenter); Mainlayout->setstretchfactor (list, 3); is set to a scalable control, the first parameter is the control that specifies the setting, and the second greater than 0 indicates that the control is scalable Mainlayout->setstretchfactor (stack, 3); Connect (list, SIGNAL (currentrowchanged (int)), stack, SLOT (setcurrentindex (int)));} Stackdlg::~stackdlg () {}
Execution Result:
"QT5 Development and Example" 5, layout management of the stack form Qstackedwidget class