//StackDlg.h
#pragma once
#include <QtGui>
class StackDlg : public QDialog
{
Q_OBJECT
public:
StackDlg(QWidget *parent = 0, Qt::WindowFlags f1 = 0);
QLabel *label1;
QLabel *label2;
QLabel *label3;
QListWidget *listwidget;
QStackedWidget *stackwidget;
};
//StackDlg.cpp
#include "stack.h"
StackDlg::StackDlg(QWidget *parent, Qt::WindowFlags f1) : QDialog(parent,f1)
{
setWindowTitle(tr("Stacked Widgets"));
listwidget = new QListWidget(this);
listwidget->insertItem(0,tr("Window 1"));
listwidget->insertItem(1,tr("Window 2"));
listwidget->insertItem(2,tr("Window 3"));
label1 = new QLabel(tr("This is Window 1 !"));
label2 = new QLabel(tr("This is Window 2 !"));
label3 = new QLabel(tr("This is Window 3 !"));
stackwidget = new QStackedWidget(this);
stackwidget->addWidget(label1);
stackwidget->addWidget(label2);
stackwidget->addWidget(label3);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
mainLayout->addWidget(listwidget,1); //延伸因子为1
mainLayout->addWidget(stackwidget,3,Qt::AlignLeft); //延伸因子为3
//mainLayout->setStretchFactor(list,1); //设置延伸因子为1
//mainLayout->setStretchFactor(stackwidget,4); //设置延伸因子为4
connect(listwidget,SIGNAL(currentRowChanged(int)),stackwidget,SLOT(setCurrentIndex(int)));
}
//main.cpp
#include <QApplication>
#include "StackDlg.h"
int main( int argc, char * argv[] )
{
QApplication a( argc, argv );
StackDlg stack;
stack.show();
return a.exec();
}