第二十四课、布局管理器(三)------------------狄泰软件学院
一、如何实现下面的用户界面
1、解决方法
(1)、绝对定位组件的坐标和大小
(2)、嵌套QBoxLayout
(3)、创建3x2的QGridLayout
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "Widget.h" #include <QLabel> #include <QLineEdit> #include <QGridLayout> Widget::Widget(QWidget *parent) : QWidget(parent, Qt::WindowCloseButtonHint) { QLabel* Name = new QLabel("Name: "); QLabel* Email = new QLabel("Email: "); QLabel* Address = new QLabel("Address: "); QLineEdit* NameEdit = new QLineEdit(); QLineEdit* EmailEdit = new QLineEdit(); QLineEdit* AddressEdit = new QLineEdit(); QGridLayout* layout = new QGridLayout(); layout->addWidget(Name, 0, 0); layout->addWidget(Email, 1, 0); layout->addWidget(Address, 2, 0); layout->addWidget(NameEdit, 0, 1); layout->addWidget(EmailEdit, 1, 1); layout->addWidget(AddressEdit, 2, 1); layout->setSpacing(10); setLayout(layout); } Widget::~Widget() { }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <QtGui/QApplication> #include "Widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
二、QFormLayout布局管理器
1、以表单(Form)的方式管理界面组件
2、表单布局中的标签和组件是相互对应的关系
3、QFormLayout的用法概要
4、QFormLayout的样式函数
5、表单布局支持嵌套,其它布局管理器可以作为子布局被其管理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "Widget.h" #include <QLabel> #include <QLineEdit> #include <QGridLayout> #include <QFormLayout> Widget::Widget(QWidget *parent) : QWidget(parent, Qt::WindowCloseButtonHint) { QLineEdit* NameEdit = new QLineEdit(); QLineEdit* EmailEdit = new QLineEdit(); QLineEdit* AddressEdit = new QLineEdit(); QFormLayout* layout = new QFormLayout(); layout->addRow("Name: ", NameEdit); layout->addRow("Email: ", EmailEdit); layout->addRow("Address: ", AddressEdit); layout->setRowWrapPolicy(QFormLayout::WrapAllRows);//设置样式 layout->setLabelAlignment(Qt::AlignRight); layout->setSpacing(10); setLayout(layout); } Widget::~Widget() { }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <QtGui/QApplication> #include "Widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
三、小结
(1)、QFormLayout以表单的方式管理界面组件
(2)、QFormLayout的样式设置简洁明了
(3)、QFormLayout支持布局管理器的相互嵌套
(4)、QFormLayout是嵌入式产品中最常用的布局方式