QStackedWidget
//window1.h
#ifndef WINDOW1_H
#define WINDOW1_H
#include <QWidget>namespace Ui {class window1;}
class window1 : public QWidget{
Q_OBJECT
public:explicit window1(QWidget *parent = 0);~window1();
private:Ui::window1 *ui;};
#endif // WINDOW1_H//window.cpp
#include "window1.h"#include "ui_window1.h"
window1::window1(QWidget *parent) :QWidget(parent),ui(new Ui::window1){
ui->setupUi(this);}
window1::~window1(){
delete ui;}
其它,window2和3一样!!再看主窗口//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>class QListWidget;class QStackedWidget;namespace Ui {class Widget;}
class Widget : public QWidget{
Q_OBJECT
public:explicit Widget(QWidget *parent = 0);~Widget();
private:Ui::Widget *ui;QListWidget* list1;QStackedWidget* stack;};
#endif // WIDGET_H//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "window1.h"
#include "window2.h"
#include "window3.h"
#include <QListWidget>
#include <QStackedWidget>
#include <QLabel>
#include <QHBoxLayout>
Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){
ui->setupUi(this);list1 = new QListWidget(this);list1->insertItem(0,"window1");list1->insertItem(1,"window2");list1->insertItem(2,"window3");window1 *win1 = new window1;window2 *win2 = new window2;window3 *win3 = new window3;stack = new QStackedWidget(this);stack->addWidget(win1);stack->addWidget(win2);stack->addWidget(win3);QHBoxLayout *mainLayout=new QHBoxLayout(this);mainLayout->setMargin(5);mainLayout->setSpacing(5);mainLayout->addWidget(list1);mainLayout->addWidget(stack);mainLayout->setStretchFactor(list1,1);//设置宽比,这里为1:3mainLayout->setStretchFactor(stack,3);
connect(list1,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));//由这块我们可以知道,stackWidget类可以加入很多个独立完整的类对象(stackWidget->addWidget(mypanel)),标记它们是靠添加时的顺序,如 0 1 2 3 ...
//显示这些对象中的某个对象时用stackWidget->setCurrentIndex(id);
}
Widget::~Widget(){
delete ui;}
效果:最后,QStackedWidget,一般和QCombox和QListWidget一起使用