布局管理器
QHBoxLayout:在水平方向排列窗口部件,从左到右;
QVBoxLayout:在垂直方向排列窗口部件,从上到下;
QGridLayout:把各个窗口部件排列在一个网格中。
1.#include "mainwindow.h"
2.#include <QApplication>
3.#include <QHBoxLayout>
4.#include <QLabel>
5.#include <QLineEdit>
6.#include <QPushButton>
7.#include <QTextCodec>
8.
9.int main(int argc, char *argv[])
10.{
11. QApplication a(argc, argv);
12. QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));//设定字符集为UTF-8
13.
14. QWidget *widget = new QWidget ;
15. QLabel *labelaccount = new QLabel(QObject::tr("帐号:"),widget);//创建帐号标签
16. QLabel *labelpassword = new QLabel(QObject::tr("密码:"),widget);//创建密码标签
17. QLineEdit *lineaccount = new QLineEdit(widget) ;//创建帐号文本输入框
18. QLineEdit *linepassword = new QLineEdit(widget) ;//创建密码文本输入框
19. QPushButton *buttonenter = new QPushButton(QObject::tr("确认"),widget);//创建确认按钮
20. QPushButton *buttonquit = new QPushButton(QObject::tr("退出"),widget);//创建退出按钮
21.
22. QHBoxLayout *layoutaccount = new QHBoxLayout ;//创建水平布局管理器
23. layoutaccount->addWidget(labelaccount);
24. layoutaccount->addWidget(lineaccount);//从左至右放置部件
25.
26. QHBoxLayout *layoutpassword = new QHBoxLayout ;
27. layoutpassword->addWidget(labelpassword);
28. layoutpassword->addWidget(linepassword);
29.
30. QVBoxLayout *layoutleft = new QVBoxLayout ;//创建一个垂直布局管理器
31. layoutleft->addStretch();//添加分隔符
32. layoutleft->addL