布局Layout

zz/2024/5/21 20:58:00

布局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.setMaximumSize3.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);//垂直布局 可以构造函数中传递widgetQHBoxLayout *lay = new QHBoxLayout();win->setLayout(lay);//win->show();//添加到QWidget作为QVBoxLayout的父类的QVBoxLayout中   QPushButton *but1 = new QPushButton("button1");lay->addWidget(but1);//水平 垂直Policy Fixed 使用推荐大小//80 : 25 80 : 25but1->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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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()
{//找到layoutQFormLayout *lay = (QFormLayout*)this->layout();if(lay == nullptr)return ;//显示错误信息的labelQLabel *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()
{//找到layoutQFormLayout *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;
}

http://www.ngui.cc/zz/2727415.html

相关文章

QTreeWidgetQTreeWidget

QTreeWidget 1.常用属性 2.标题设置 3.内容插入 4.内容选择 5.拖动和删除 6.信号事件 7.样式qss QTreeWidget 属性 1.header()->setVisible(true) 2.setSortingEnabled点击标题排序 3.setAnimated 动画展开 4.setVerticalScrollBarPolicy setHorizontalScrollBarPolicy滚动…

JDK 和 JRE 的区别

JRE(Java Runtime Enviroment) 是 Java 的运行环境。面向 Java 程序的使用者&#xff0c;而不是开发者。如果你仅下载并安装了JRE&#xff0c;那么你的系统只能运行 Java 程序。JRE 是运行 Java 程序所必须环境的集合&#xff0c;包含JVM标准实现及 Java 核心类库。它包括 Java…

通信__协议的那点事!!

我们的“协议”&#xff1a; 到目前为止&#xff0c;我们已经简单了解了通信的基本模型&#xff0c;Server—Client模型&#xff0c;这里以简单聊天工具为例&#xff1a;1、服务端启动——2、客户端启动&#xff0c;并试图与服务端建立连接——3、服务端根据条件&#xff08;通…

《Google三大论文》_The Google File System

关于预期和实现&#xff1a; 在Google的面向大规模的数据密集型的分布式运用中&#xff0c;GFS都是关键和基础。同时&#xff0c;开源的hadoop File System 与GFS也是很相似的。 与传统的文件系统相比&#xff0c;GFS在设计上有以下几种特点&#xff1a; 一、组件失效被认为是常…

日记 or 小小说 :想进腾讯的师弟师妹们,别学我 (三)

建议您先看&#xff1a;日记 or 小小说 &#xff1a;想进腾讯的师弟师妹们&#xff0c;别学我 &#xff08;一&#xff09;&#xff08;二&#xff09; ~~~~~~~~~~~~~~~~~~~~~我是分割符号~~~~~~~~~ 每一个值得被历史铭记的时刻总是会出现一些不能用科学来解释的现象的&#xf…

日记 or 小小说 :想进腾讯的师弟师妹们,别学我 (四)

请原谅我用这样的标题将你们的注意力引来&#xff0c;其实这只是因为我想不到更好的标题了。 突然想起来文章可能会涉及到个人隐私&#xff08;虽然我一直宣称是小说&#xff0c;文中人物是虚构的&#xff0c;但骗自己有意思么&#xff1f;&#xff09;&#xff0c;再加上最近发…

突然发现的HDFS与GFS在实现上的一些不同

GFS&#xff1a;Google File System HDFS&#xff1a;Hadoop Distribute File System 首先&#xff0c;有一点要确认的是&#xff0c;作为GFS的一个最重要的实现&#xff0c;HDFS设计目标和GFS是高度一致的。在架构、块大小、元数据等的实现上&#xff0c;HDFS与GFS大致一致。但…

在SP公司打工,服务器端技术很重要

所以&#xff0c;既然要做点事情&#xff0c;先从Tomcat开始:) 新手&#xff0c;不知道怎么写blog&#xff0c;大家看了多包涵&#xff0c;自己努力ing

[FW:] Writing big files to ServletOutputStream

Problem when writing big files to ServletOutputStream 转自&#xff1a; http://www.theserverside.com/discussions/thread.tss?thread_id29261 Posted By: Anand Rajagopal on October 06, 2004 10:18 AM I am using the following code to write files to servlet outp…

创建动态菜单的JSP自定义标签(A Custom JSP Tag Library for Dynamic Menus)

转自&#xff1a; http://www.onjava.com/pub/a/onjava/2003/04/09/dynamic_taglib.html
最新文章