#include "widget.h"#include "ui_widget.h"#include <QStackedWidget>#include <QVBoxLayout>#include <QComboBox>Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ QWidget *firstPageWidget = new QWidget; QWidget *secondPageWidget = new QWidget; QWidget *thirdPageWidget = new QWidget; QStackedWidget *stackedWidget = new QStackedWidget(); stackedWidget->addWidget(firstPageWidget); stackedWidget->addWidget(secondPageWidget); stackedWidget->addWidget(thirdPageWidget); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(stackedWidget); setLayout(layout); ui->setupUi(this);}void Widget::click(){ // ???}Widget::~Widget(){ delete ui;}
stackedWidget->setCurrentIndex(2);
C++ (Qt)class QSW : public QStackedWidget{public: QSW(QWidget *parent = 0) : QStackedWidget(parent) { addWidget(new QLabel("Page 1", this)); addWidget(new QLabel("Page 2", this)); addWidget(new QLabel("Page 3", this)); setCurrentIndex(1); }}; int main(int argc, char *argv[]){ QApplication app(argc, argv); QSW *qsw = new QSW; qsw->show(); return app.exec();}