1、水平布局:QHBoxLayout
垂直布局:QVBoxLayout
2、实现登录注册页面
//设置窗口大小
this->setFixedSize(400,300);
this->titleLabel = new QLabel;
this->titleLabel->setText(QStringLiteral("登录"));
//对齐方式setAlignment
this->titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//布局策略 setSizePolicy设置大小策略(水平、垂直)
this->titleLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
//this->titleLabel->setGeometry(180,40,40,40);手动布局,设置控件大小
this->userLael = new QLabel;
this->userLael->setText(QStringLiteral("用户"));
//布局策略
this->userLael->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
//this->userLael->setGeometry(60,80,30,20);手动布局,设置控件大小
this->passwdLabel = new QLabel;
this->passwdLabel->setText(QStringLiteral("密码"));
//布局策略
this->passwdLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
this->userEdit = new QLineEdit;
this->passwdEdit = new QLineEdit;
this->userEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
this->passwdEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
//设置模式,一般是用小黑点覆盖你所输入的字符
this->passwdEdit->setEchoMode(QLineEdit::Password);
this->autoLog = new QCheckBox;
this->savePasswd = new QCheckBox;
this->logButton = new QPushButton;
logButton->setText(QStringLiteral("登录"));
this->regButton = new QPushButton;
regButton->setText(QStringLiteral("注册"));
this->logButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
this->regButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
autoLog->setText(QStringLiteral("自动登录"));
//布局策略
this->autoLog->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
savePasswd->setText(QStringLiteral("记住密码"));
//布局策略
this->savePasswd->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
//布局管理器:水平布局
this->layout1 = new QHBoxLayout;
this->layout2 = new QHBoxLayout;
this->layout3 = new QHBoxLayout;
this->layout4 = new QHBoxLayout;
//addwidget()方法用于向布局中添加控件
this->layout1->addWidget(this->userLael);
this->layout1->addWidget(this->userEdit);
this->layout2->addWidget(this->passwdLabel);
this->layout2->addWidget(this->passwdEdit);
this->layout3->addStretch();//增加伸缩量
this->layout3->addWidget(this->autoLog);
this->layout3->addStretch();//增加伸缩量
this->layout3->addWidget(this->savePasswd);
this->layout3->addStretch();//增加伸缩量
this->layout4->addWidget(this->logButton);
this->layout4->addWidget(this->regButton);
//布局管理器:垂直布局
this->vlayout = new QVBoxLayout;
//addLayout()方法用于向布局中添加子布局
//addLayout用于添加一个已经包含另外一个QLayout布局的盒子到行(或列)中,同时设置布局的拉伸因子
this->vlayout->addWidget(this->titleLabel);//1
this->vlayout->addLayout(this->layout1);//2
this->vlayout->addLayout(this->layout2);//2
this->vlayout->addLayout(this->layout3);//1
this->vlayout->addLayout(this->layout4);//2
//QHBoxLayout::setStretch(int index, int stretch)
//水平布局添加的控件按照比例进行伸缩
//两个控件索引0、1、2、3、4,分别按照1:2:2:1:2比例进行伸缩
this->vlayout->setStretch(0,1);
this->vlayout->setStretch(1,2);
this->vlayout->setStretch(2,2);
this->vlayout->setStretch(3,1);
this->vlayout->setStretch(4,2);
setLayout(vlayout);
setFixedSize(400,300)设置窗口大小
setSizePolicy设置大小策略(水平、垂直)
对齐方式setAlignment
setGeometry(60,80,30,20);手动布局,设置控件大小
setEchoMode(QLineEdit::Password)设置模式,一般是用小黑点覆盖你所输入的字符
addStretch();//增加伸缩量
addwidget()方法用于向布局中添加控件
addLayout()方法用于向布局中添加子布局
addLayout用于添加一个已经包含另外一个QLayout布局的盒子到行(或列)中,同时设置布局的拉伸因子
setStretch(int index, int stretch)水平布局添加的控件按照比例进行伸缩
//设置内部控件与边框的距离
void setContentsMargins(int left, int top, int right, int bottom)
//设置两个子控件之间的间距
void setSpacing(int)
//设置水平布局器元素间距为20像素点
hlay->setSpacing(20);