一、佈局管理器的綜合實例------模擬嚮導用戶界面(Windows平臺)ide
-----練習開發一個嚮導用戶界面函數
@1:在同一界面上展示不一樣的嚮導頁面佈局
@2:經過上一步和下一步按鈕進行切換ui
@3:不一樣頁面上的元素組件和這些組件排布都不相同this
@4:頁面中的組件經過佈局管理進行排布spa
(1)經過佈局嵌套進行界面設計設計
@1:上一步和下一步這兩個按鈕用水平佈局管理器QHBoxLayout來進行管理,不一樣頁面上的顯示的內容只有按鈕不變,因此講不一樣頁面的內容用棧式佈局管理器QStackedLayout進行管理,這樣orm
在切換不能頁面的時候,就至關於切換棧式佈局管理器中的棧頂組件。以後用垂直佈局管理器QVBoxLayout將這個棧式佈局管理器和水平佈局管理器進行管理,就達到了目的要求了內存
@2:經過QStackedLayout棧式佈局管理器管理不一樣的頁面。每個頁面都是一個QWidget的組件,每個Qwidget組件中的內容都是不同的,每個QWidget組件做爲一個容器,這個容器開發
中都有一個佈局管理器或多個佈局管理器
二、注意事項:
(1)任意容器類的組件均可以指定佈局管理器
(2)同一佈局管理器中的組件擁有相同的父組件
(3)設置佈局管理器的同時也隱式的指定了父子關係了
(4)組件間的父子關係是Qt中內存管理的重要方式
/************************************************************.h代碼************************************************************************/
#ifndef _WIDGET_H_
#define _WIDGET_H_
#include <QtGui/QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedLayout>
#include <QLabel>
#include <QLineEdit>
#include <QFormLayout>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton preButton;
QPushButton nextButton;
QLabel label1; //標籤
QLabel label2;
QLabel label3;
QLabel label4;
QLineEdit LineEdit1;
QLineEdit LineEdit2;
QPushButton button1;
QPushButton button2;
QStackedLayout slayout; //定義一個棧式佈局管理器,在類中定義以便後面的成員函數訪問方便
void init(); //用來初始化構造界面的
QWidget *get_Frist_Widget(); //打造QStackedLayout棧式佈局管理器的第1頁面的函數
QWidget *get_Second_Widget(); //打造QStackedLayout棧式佈局管理器的第2頁面的函數
QWidget *get_Thrid_Widget(); //打造QStackedLayout棧式佈局管理器的第3頁面的函數
private slots:
void PreButtonClicked();
void NextButtonClicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // _WIDGET_H_
/*******************************************************.cpp的代碼*************************************************************************/
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent), preButton(this), nextButton(this)
{
init();
}
void Widget::init()
{
QVBoxLayout *vlayout = new QVBoxLayout(); //建立一個垂直佈局管理器
QHBoxLayout *hlayout = new QHBoxLayout(); //建立一個水平佈局管理器
preButton.setText("Pre Page");
preButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); //設置改按鈕行爲擴展,列爲固定,有佈局管理器時起做用
preButton.setMinimumSize(160, 30); //設置該按鈕最小爲160*30像素
nextButton.setText("Next Page");
nextButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); //設置改按鈕行爲擴展,列爲固定,有佈局管理器時起做用
nextButton.setMinimumSize(160, 30); //設置該按鈕最小爲160*30像素
connect(&preButton, SIGNAL(clicked()), this, SLOT(PreButtonClicked()));
connect(&nextButton, SIGNAL(clicked()), this, SLOT(NextButtonClicked()));
hlayout->addWidget(&preButton); //水平佈局管理器管理按鈕
hlayout->addWidget(&nextButton);
slayout.addWidget(get_Frist_Widget()); //將寫好的不一樣QWiget組件頁面添加到QStackedLayout棧式佈局管理器中管理。 0
slayout.addWidget(get_Second_Widget()); // 1
slayout.addWidget(get_Thrid_Widget()); // 2
vlayout->addLayout(&slayout); //讓這個垂直佈局管理器管理這個水平佈局管理器
vlayout->addLayout(hlayout); //讓這個垂直佈局管理器管理這個棧式佈局管理器
slayout.setCurrentIndex(0); //設置當前棧式佈局管理器中顯示的組件是第1個
this->setLayout(vlayout); //設置當前窗口的佈局管理器爲vlayout這個垂直佈局管理器
}
QWidget* Widget::get_Frist_Widget() //打造QStackedLayout棧式佈局管理器的第1頁面的函數
{
QWidget *ret = new QWidget();
QGridLayout *layout = new QGridLayout(); //網格管理形式的佈局管理器
label1.setText("WuHengYi"); //設置標籤的內容
label2.setText("FangQingQing");
label3.setText("I LOVE YOU");
label4.setText("Forever");
layout->addWidget(&label1, 0, 0); //設置標籤在0,0這個格子的位置
layout->addWidget(&label2, 0, 1);
layout->addWidget(&label3, 1, 0);
layout->addWidget(&label4, 1, 1);
ret->setLayout(layout); //將要返回的這個QWidget容器組件中的佈局管理器設置爲layout
return ret;
}
QWidget* Widget::get_Second_Widget() //打造QStackedLayout棧式佈局管理器的第2頁面的函數
{
QWidget *ret = new QWidget();
QFormLayout *layout = new QFormLayout(); //表單形式的佈局管理器
LineEdit1.setText("So care about you");
LineEdit2.setText("FangQingQing");
layout->addRow("who:", &LineEdit1); //設置表單,前面一個參數是標籤,後面一個是編輯框,二者是相互對應的。不會隨着窗口變,二者距離變
layout->addRow("Love to:", &LineEdit2);
ret->setLayout(layout); //將要返回的這個QWidget容器組件中的佈局管理器設置爲layout
return ret;
}
QWidget* Widget::get_Thrid_Widget() //打造QStackedLayout棧式佈局管理器的第3頁面的函數
{
QWidget *ret = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
button1.setText("this is");
button2.setText("family");
layout->addWidget(&button1); //將按鈕1添加到這個垂直佈局管理器中
layout->addWidget(&button2);
ret->setLayout(layout);
return ret;
}
//點擊這個按鈕,到上一個頁面(QStackedLayout佈局管理器管理的組件)
void Widget::PreButtonClicked()
{
int index = ((slayout.currentIndex() - 1) + 3) % 3; //由於顯示上一個頁面,-1怕減多了,由於3個頁面(QWidget),因此+3對3取餘
slayout.setCurrentIndex(index); //顯示上一個頁面
}
void Widget::NextButtonClicked()
{
int index = (slayout.currentIndex() + 1) % 3; //由於顯示下一個頁面, 由於3個頁面(QWidget),因此+1對3取餘
slayout.setCurrentIndex(index); //顯示下一個頁面
}
Widget::~Widget()
{
}
/*****************************************************************main.cpp**************************************************************/
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}