1. 布局管理器提供相关的类对界面组件进行布局管理,能够自动排列窗口中的界面组件,窗口变化后能自动更新界面组件的大小。
2. QLayout是Qt布局管理器的抽象基类,通过继承QLayout实现了功能各异且互补的布局管理器。
①QBoxLayout: QVBoxLayout, QHBoxLayout
②QGridLayout:
③QFormLayout:
④QStackedLayout:
3. 综合实例(一个简单的安装向导模型)
Widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> #include <QPushButton> #include <QLabel> #include <QLineEdit> class Widget : public QWidget { Q_OBJECT private: QPushButton PreBtn; QPushButton NextBtn; QLabel Lab0; QLabel Lab1; QLabel Lab2; QLabel Lab3; QPushButton PushButton1; QPushButton PushButton2; QLineEdit LineEdit; void InitControl(); QWidget* GetFirstPage(); QWidget* GetSecondPage(); QWidget* GetThirdPage(); private slots: void PreBtnClicked(); void NextBtnClicked(); public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
Widget.cpp
#include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> #include <QFormLayout> #include <QStackedLayout> #include <QDebug> #include "widget.h" Widget::Widget(QWidget *parent): QWidget(parent), PreBtn(this), NextBtn(this) { InitControl(); } void Widget::InitControl() { QVBoxLayout* vLayout = new QVBoxLayout(); QHBoxLayout* hLayout = new QHBoxLayout(); QStackedLayout* sLayout = new QStackedLayout(); PreBtn.setText("Pre Button"); PreBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); PreBtn.setMinimumSize(160, 30); NextBtn.setText("Next Button"); NextBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); NextBtn.setMinimumSize(160, 30); sLayout->addWidget(GetFirstPage()); sLayout->addWidget(GetSecondPage()); sLayout->addWidget(GetThirdPage()); hLayout->setSpacing(10); hLayout->addWidget(&PreBtn); hLayout->addWidget(&NextBtn); vLayout->addLayout(sLayout); vLayout->addLayout(hLayout); setLayout(vLayout); connect(&PreBtn, SIGNAL(clicked()), this, SLOT(PreBtnClicked())); connect(&NextBtn, SIGNAL(clicked()), this, SLOT(NextBtnClicked())); } QWidget* Widget::GetFirstPage() { QWidget* widget = new QWidget(); QGridLayout* gLayout = new QGridLayout(); Lab0.setText("This"); Lab1.setText("is"); Lab2.setText("First"); Lab3.setText("Page"); gLayout->addWidget(&Lab0, 0, 0); gLayout->addWidget(&Lab1, 0, 1); gLayout->addWidget(&Lab2, 1, 0); gLayout->addWidget(&Lab3, 1, 1); widget->setLayout(gLayout); return widget; } QWidget* Widget::GetSecondPage() { QWidget* widget = new QWidget(); QFormLayout* fLayout = new QFormLayout(); LineEdit.setText("This is Second Page"); fLayout->addRow("Name", &LineEdit); widget->setLayout(fLayout); return widget; } QWidget* Widget::GetThirdPage() { QWidget* widget = new QWidget(); QVBoxLayout* vLayout = new QVBoxLayout(); PushButton1.setText("Third"); PushButton2.setText("Page"); vLayout->addWidget(&PushButton1); vLayout->addWidget(&PushButton2); widget->setLayout(vLayout); return widget; } void Widget::PreBtnClicked() { QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[0]); if(sLayout != NULL) { int currentIndex = sLayout->currentIndex(); currentIndex = (currentIndex > 0) ? (currentIndex - 1) : currentIndex; sLayout->setCurrentIndex(currentIndex); } qDebug() << "PreBtnClicked()"; } void Widget::NextBtnClicked() { QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[0]); if(sLayout != NULL) { int currentIndex = sLayout->currentIndex(); currentIndex = (currentIndex < 3) ? (currentIndex + 1) : currentIndex; sLayout->setCurrentIndex(currentIndex); } qDebug() << "NextBtnClicked()"; } Widget::~Widget() { }