设计思路:
如下图所示,垂直布局管理器里面嵌入栈式布局管理器和水平布局管理器,栈式布局管理器里面再嵌入网格布局管理器、表格布局管理器、垂直布局管理器
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QFormLayout>
#include <QStackedLayout>
#include <QTimer>
class Widget : public QWidget
{
Q_OBJECT
private slots:
void timeFlow();
void prevBtnClicked();
void nextBtnClicked();
public:
Widget(QWidget *parent = 0);
QStackedLayout slayout;
QLabel lab1;
QLabel lab2;
QLabel lab3;
QLabel lab4;
QPushButton prev;
QPushButton next;
QWidget* getFirstPage();
QWidget* getSecondPage();
QWidget* getThirdPage();
void VHBoxLayout();
void BoxLayout();
~Widget();
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent), lab1(this), lab2(this), lab3(this), lab4(this),
prev(this), next(this)
{
BoxLayout();
}
void Widget::timeFlow()
{
}
void Widget::BoxLayout()
{
QVBoxLayout* vlayout = new QVBoxLayout();
//QStackedLayout* slayout = new QStackedLayout();
QHBoxLayout* hlayout = new QHBoxLayout();
connect(&prev, SIGNAL(clicked(bool)), this, SLOT(prevBtnClicked()));
connect(&next, SIGNAL(clicked(bool)), this, SLOT(nextBtnClicked()));
slayout.addWidget(getFirstPage());
slayout.addWidget(getSecondPage());
slayout.addWidget(getThirdPage());
prev.setText("prev");
next.setText("next");
hlayout->addWidget(&prev);
hlayout->addWidget(&next);
vlayout->addLayout(&slayout);
vlayout->addLayout(hlayout);
setLayout(vlayout);
setWindowTitle("test");
}
QWidget* Widget::getFirstPage()
{
QGridLayout* glayout = new QGridLayout();
QWidget* widget = new QWidget();
lab1.setText("this");
lab2.setText("is");
lab3.setText("first");
lab4.setText("page");
glayout->addWidget(&lab1, 0, 0);
glayout->addWidget(&lab2, 0, 1);
glayout->addWidget(&lab3, 1, 0);
glayout->addWidget(&lab4, 1, 1);
widget->setLayout(glayout);
return widget;
}
QWidget* Widget::getSecondPage()
{
QLineEdit* lineEdit = new QLineEdit();
QWidget* widget = new QWidget();
QFormLayout* flayout = new QFormLayout();
lineEdit->setText("this is second page");
flayout->addRow("Hint:", lineEdit);
widget->setLayout(flayout);
return widget;
}
QWidget* Widget::getThirdPage()
{
QPushButton* btn1 = new QPushButton();
QPushButton* btn2 = new QPushButton();
QVBoxLayout* vlayout = new QVBoxLayout();
QWidget* widget = new QWidget();
btn1->setText("this is");
btn2->setText("third page");
vlayout->addWidget(btn1);
vlayout->addWidget(btn2);
vlayout->setSpacing(10);
widget->setLayout(vlayout);
return widget;
}
void Widget::prevBtnClicked()
{
int index = ((slayout.currentIndex()-1)+3)%3;
slayout.setCurrentIndex(index);
}
void Widget::nextBtnClicked()
{
int index = (slayout.currentIndex()+1)%3;
slayout.setCurrentIndex(index);
}
Widget::~Widget()
{
}
//main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
输出: