//stackdlg.h
#ifndef STACKDLG_H
#define STACKDLG_H
#include <QWidget>
#include <QListWidget>
#include <QStackedWidget>
#include <QLabel>
#include <QHBoxLayout>
namespace Ui {
class StackDlg;
}
class StackDlg : public QWidget
{
Q_OBJECT
public:
explicit StackDlg(QWidget *parent = 0);
~StackDlg();
private:
Ui::StackDlg *ui;
private:
QListWidget *list;
QStackedWidget *stack;
QLabel* label1;
QLabel* label2;
QLabel* label3;
};
#endif // STACKDLG_H
//stackdlg.cpp
#include "stackdlg.h"
#include "ui_stackdlg.h"
StackDlg::StackDlg(QWidget *parent) :
QWidget(parent),
ui(new Ui::StackDlg)
{
ui->setupUi(this);
setWindowTitle(tr("StackedWidget"));
list=new QListWidget(this);
list->insertItem(0,tr("window1"));
list->insertItem(1,tr("window2"));
list->insertItem(2,tr("window4"));
label1=new QLabel(tr("WidowTest1"));
label2=new QLabel(tr("WidowTest2"));
label3=new QLabel(tr("WidowTest3"));
//新建一个堆栈窗体对象
stack=new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout=new QHBoxLayout(this);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignHCenter);
//设定可伸缩控件,第一个参数用于指定设定的控件,第二参数大于0,表示此控件为可伸缩控件(占的比例)
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
//堆栈窗体index按插入顺序从0起依次排序
connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
}
StackDlg::~StackDlg()
{
delete ui;
}