1 // 3.3 堆栈窗体 QStackedWidget类
2 //stackdlg.h
3 #ifndef STACKDLG_H
4 #define STACKDLG_H
5
6 #include <QDialog>
7 #include <QListWidget>
8 #include <QStackedWidget>
9 #include <QLabel>
10 class StackDlg : public QDialog
11 {
12 Q_OBJECT
13
14 public:
15 StackDlg(QWidget *parent = 0);
16 ~StackDlg();
17 private:
18 QListWidget *list;
19 QStackedWidget *stack;
20 QLabel *label1;
21 QLabel *label2;
22 QLabel *label3;
23 };
24
25 #endif // STACKDLG_H
26 //stackdlg.cpp
27 #include "stackdlg.h"
28 #include <QHBoxLayout>
29 StackDlg::StackDlg(QWidget *parent)
30 : QDialog(parent)
31 {
32 setWindowTitle(tr("StackedWidget"));
33 list =new QListWidget(this);//新建一个QListWidget对象
34 list->insertItem(0,tr("Window1"));//插入条目
35 list->insertItem(1,tr("Window2"));
36 list->insertItem(2,tr("Window3"));
37 label1 =new QLabel(tr("WindowTest1"));//创建标签控件对象
38 label2 =new QLabel(tr("WindowTest2"));
39 label3 =new QLabel(tr("WindowTest3"));
40 stack =new QStackedWidget(this);//新建堆栈窗体对象
41 stack->addWidget(label1);//将标签控件插入到堆栈窗体中
42 stack->addWidget(label2);
43 stack->addWidget(label3);
44 QHBoxLayout *mainLayout =new QHBoxLayout(this);//对整个对话框进行布局
45 mainLayout->setMargin(5);//对话框边距
46 mainLayout->setSpacing(5);//控件之间的间距
47 mainLayout->addWidget(list);
48 mainLayout->addWidget(stack,0,Qt::AlignHCenter);
49 mainLayout->setStretchFactor(list,1);
50 mainLayout->setStretchFactor(stack,1);
51 connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
52
53 }
54
55 StackDlg::~StackDlg()
56 {
57
58 }