布局Layout
1.Vertical Layout
2.Horizontal Layout
3.Grid Layout
4.Form Layout
sizeHint推荐尺寸
1.QSize sizeHint() 推荐尺寸只能重载修改
2.QSize size() 不包含边框的窗口尺寸
QSizePolicy::PolicyFlag
1.GrowFlag 必要时可超过推荐
2.ExpandFlag 尽可能的扩展
3.ShrinkFlag 必要时可小于推荐
4.IgnoreFlag 缺省大小被忽略
QSizePolicy::Policy
1.Fixed 0 只参考sizeHint()
2.Minimum GrowFlag 伸张和收缩 >= sizeHint()
3.Maximum ShrinkFlag 伸张和收缩 <= sizeHint()
4.Preferred GrowFlag | ShrinkFlag 默认
5.Expanding GrowFlag | ShrinkFlag | ExpandFlag
6.MinimumExpanding GrowFlag | ExpandFlag
7.Ignored ShrinkFlag | GrowFlag | IgnoreFlag
setContentsMargins
1.(int left,int top,int right,int bottom)
setSizePolicy
1.(QSizePolicy::Policy horizontal,QSizePolicy::Policy vertical)
设置最大最小尺寸
1.setMinimumSize
2.setMaximumSize
3.setSpacing(6); //间距
4.setContentsMargins(11,11,11,11);//边框距离
//#include "widget.h"
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win = new QWidget();
win->resize(400,400);
//垂直布局 可以构造函数中传递widget
QHBoxLayout *lay = new QHBoxLayout();
win->setLayout(lay);
//win->show();
//添加到QWidget作为QVBoxLayout的父类的QVBoxLayout中
QPushButton *but1 = new QPushButton("button1");
lay->addWidget(but1);
//水平 垂直Policy Fixed 使用推荐大小
//80 : 25 80 : 25
but1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
//水平拉伸 垂直拉伸
//80 : 25 400 : 338
//尽量最大最小尺寸为推荐尺寸
but1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
//忽略推荐大小,尽量缩放
but1->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
but1->setMinimumSize(100,50);
but1->setMaximumSize(500,100);
QPushButton *but2 = new QPushButton("button2");
lay->addWidget(but2);
QPushButton *but3 = new QPushButton("button3");
lay->addWidget(but3);
//边框距离
lay->setContentsMargins(0,0,0,0);
//元素间的距离
lay->setSpacing(0);
win->show();
//没有设置setContentsMargins(0,0,0,0);
//{80 : 25} {378 : 25}
//设置setContentsMargins(0,0,0,0);
//{80 : 25} {400 : 25}
qDebug()<<but1->sizeHint().width()<<":"<<but1->sizeHint().height();
qDebug()<<but1->width()<<":"<<but1->height();
return a.exec();
}
QGridLayout
void addWidget(
QWidget*,
int row,
int column,
Qt::Aignment = Qt::Alignment()
)
AlignmentFlag 对齐
AlignLeft = 0x0001
AlignLeading = AlignLeft
AlignRight = 0x0002
AlignTrailing = AlignRight
AlignHCenter = 0x0004
AligJustify = 0x0008
AlignAbsolute = 0x0010
AlignHorizontal_Mask = AlignLeft | AlignRight | AlignHCenter | AligJustify | AlignAbsolute
AlignTop = 0x0020
setSpacing
1.setHorizontalSpacing(100);
2.setVerticalSpacing(50);
3.setSpacing(8);
#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win = new QWidget;
QGridLayout *gl = new QGridLayout(win);
QPushButton *but11 = new QPushButton("but11");
gl->addWidget(but11,0,0);
QPushButton *but12 = new QPushButton("but12");
gl->addWidget(but12,0,1);
but12->setMinimumSize(100,100);
but12->setMaximumSize(500,500);
but12->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
QPushButton *but13 = new QPushButton("but13");
gl->addWidget(but13,0,2);
QPushButton *but21 = new QPushButton("but21");
gl->addWidget(but21,1,0);
QPushButton *but23 = new QPushButton("but23");
gl->addWidget(but23,1,2);
//水平间距
gl->setHorizontalSpacing(100);
//垂直间距
gl->setVerticalSpacing(10);
win->show();
return a.exec();
}
QFormLayout
1.插入项目addRow 和 insertRow
2.遍历项目
3.表单提交验证示例
QFormLayout addRow
1.QFormLayout *formLayout = new QFormLayout;
2.formLayout->addRow(tr("&Name:"),nameLineEdit);
3.formLayout->addRow(tr("&Email:"),emailLineEdit);
4.formLayout->addRow(tr("&Age:"),ageSpinBox);
5.setLayout(formLayout);
布局器中添加布局器
1.void QFormLayout::addRow(const QString &labelText,QLayout *field);
QFormLayout insertRow
1.void QFormLayout::insertRow
(int row,
const QString &labelText,
QWidget *field)
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void Save();
void Clear();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QFormLayout *lay = new QFormLayout(this);
QLineEdit *name = new QLineEdit();
lay->addRow("&Name",name);
QLineEdit *email = new QLineEdit();
lay->addRow("&Email",email);
QLineEdit *age = new QLineEdit();
lay->insertRow(1, "&Age", age);
//显示出错信息
QLabel *info = new QLabel();
info->setText("input");
lay->insertRow(0,"",info);
//设置样式表
info->setStyleSheet("color:red");
lay->insertRow(0,"",info);
//添加Save 和 Clear按钮
QPushButton *save = new QPushButton("Save");
QPushButton *clear = new QPushButton("Clear");
QObject::connect(save,&QPushButton::clicked,this,&Widget::Save);
QObject::connect(clear,&QPushButton::clicked,this,&Widget::Clear);
//添加一个平行布局器
QHBoxLayout *hlay = new QHBoxLayout();
hlay->addWidget(save);
hlay->addWidget(clear);
lay->addRow(hlay);
}
void Widget::Save()
{
//找到layout
QFormLayout *lay = (QFormLayout*)this->layout();
if(lay == nullptr)
return ;
//显示错误信息的label
QLabel *label = (QLabel*)lay->itemAt(0,QFormLayout::FieldRole)->widget();
//存放错误信息
QString error = "";
//遍历QFormLayout的所有元素
for(int i = 0;i < lay->rowCount();i++)
{
// qDebug()<<lay->rowCount()<<" "<<i;
QLayoutItem * item = lay->itemAt(i,QFormLayout::FieldRole);
QLayoutItem * layItem = lay->itemAt(i,QFormLayout::LabelRole);
if(!item || !layItem) continue;
QLineEdit *e = (QLineEdit*)item->widget();
if(!e) continue;
QString cname = e->metaObject()->className();
QLabel *titleLabel = (QLabel*)layItem->widget();
if(!titleLabel) continue;
QString title = titleLabel->text();
// qDebug()<<cname;
if(cname == "QLineEdit")
{
if(e->text().trimmed() == "")
{
e->setFocus();
error += title;
error += " is empty!\n";
}
}
}
//设置错误信息
label->setText(error);
}
void Widget::Clear()
{
//找到layout
QFormLayout *lay = (QFormLayout*)this->layout();
if(lay == nullptr)
return ;
//遍历QFormLayout的所有元素
for(int i = 0;i < lay->rowCount();i++)
{
// qDebug()<<lay->rowCount()<<" "<<i;
QLayoutItem * item = lay->itemAt(i,QFormLayout::FieldRole);
if(item == nullptr)
continue;
QLineEdit *e = (QLineEdit*)item->widget();
if(!e)
continue;
QString cname = e->metaObject()->className();
// qDebug()<<cname;
if(cname == "QLineEdit")
{
e->setText("");
}
}
}
Widget::~Widget()
{
delete ui;
}