Qt之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)
QBoxLayout可以在水平方向或垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所继承。
QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列。
QVBoxLayout:垂直布局,在垂直方向上排列控件,即:上下排列。
通过查看源码,我们可以发现,水平布局、垂直布局除了构造时的方向(LeftToRight、TopToBottom)不同外,其它均相同。
下面我们以QHBoxLayout为例,来讲解QBoxLayout的常用功能。
QPushButton *pButton1 = new QPushButton("One"); QPushButton *pButton2 = new QPushButton("Two"); QPushButton *pButton3 = new QPushButton("Three"); QPushButton *pButton4 = new QPushButton("Four"); QPushButton *pButton5 = new QPushButton("Five"); QHBoxLayout *pHLayout = new QHBoxLayout(); pHLayout- addWidget(pButton1); pHLayout- addWidget(pButton2); pHLayout- addWidget(pButton3); pHLayout- addWidget(pButton4); pHLayout- addWidget(pButton5); setLayout(pHLayout);
setContentsMargins(int left, int top, int right, int bottom); setContentsMargins(const QMargins margins)
设置外边距
setMargin可以设置左、上、右、下的外边距,设置之后,他们的外边距是相同的。
setContentsMargins与其功能相同,但是可以将左、上、右、下的外边距设置为不同的值。
这里我使用setMargin(10)将外边距设置为10。
一般情况下,会有一个默认间距值,为了保持所有布局的统一性,或者你需要一个更合适的间距值,则需要手动设置。
这里我使用setSpacing(0)将间距设置为0。
QHBoxLayout *pHLayout = new QHBoxLayout(); pHLayout- addStretch(); // 第一个控件之前添加伸缩 pHLayout- addWidget(pButton1); pHLayout- addWidget(pButton2); pHLayout- addWidget(pButton3); pHLayout- addWidget(pButton4); pHLayout- addWidget(pButton5); pHLayout- addStretch(); // 最后一个控件之后添加伸缩 pHLayout- setSpacing(10);
均分
在每一个控件之间都添加伸缩,这样所有的控件之间的间距都会相同。
其中有控件大小不相同的时候就会看得很明显了,如果我们需要将其中的某些控件居上、居下显示,那么可以使用对齐方式Qt::Alignment。
下面,我们使用向上、向下对齐来设置其它控件。
pHLayout- addWidget(pButton1, 0 , Qt::AlignLeft | Qt::AlignTop); pHLayout- addWidget(pButton2, 0 , Qt::AlignLeft | Qt::AlignTop); pHLayout- addWidget(pButton3); // 水平居左 垂直居下 pHLayout- addWidget(pButton4, 0 , Qt::AlignLeft | Qt::AlignBottom); pHLayout- addWidget(pButton5, 0 , Qt::AlignLeft | Qt::AlignBottom); pHLayout- setSpacing(10);
setDirection(QBoxLayout::TopToBottom);
既然使用了QHBoxLayout,一般就不建议使用TopToBottom或者BottomToTop,如果实在确定不了方向,或者方向可以随意变化,那么建议使用QBoxLayout。
setStretchFactor(QWidget *w, int stretch); setStretchFactor(QLayout *l, int stretch);
设置控件、布局的拉伸系数
当窗体大小变化时,控件会根据拉伸系数来做相应的调整。
setStretchFactor(pButton1, 1);
setStretchFactor(pButton2, 2);
设置pButton1的拉伸系数为1,pButton2拉伸系数为2,当窗体变大时,会优先将pButton2进行拉伸,当达到一定程度时,再拉伸pButton1,pButton1与pButton2的宽度比例为1:2。
上面介绍了基本所有常用的接口使用,还有一些inset…接口,和它们功能相同,只不过是需要传递控件所在的索引index。常用的这些接口掌握了,其它布局QVBoxLayout、QGridLayout功能也相同或类似,一通百通。
Flex 布局与传统布局的比较 在前端开发中,页面的布局是一个非常关键的问题。传统的布局方式,如使用浮动和定位来实现布局,虽然可以实现一定的布局效果,但是代码量较多,可维护性较差,难以实现复杂的布局需求。
相关文章
- Qt-QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connect...
- Qt使用cos、sin绘制圆
- Qt-QProgressBar的QSS
- QT-打破布局,还原布局的方法(比较另类,呵呵)
- Qt音视频开发13-mpv录像存储
- Qt编写linux上视频流播放器(支持海康大华宇视等各种网络摄像机)
- Qt编写安防视频监控系统12-异形布局
- Qt编写可视化大屏电子看板系统11-自定义控件
- Qt布局交换两个控件位置
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
- qt 5.15 model如何改变欧拉旋转的旋转点
- Qt QDebug格式输出、自定义类输出及重定向输出Log
- Qt对话框及其类型、布局管理器
- Qt之网格布局
- 浅析 Qt 布局系统
- Qt线程的生命周期
- Qt自定义进度条
- C++Qt 水平布局(QHBoxLayout)
- Qt的QFile
- QT多线程实战_Qt多线程开发项目
- Qt开发-QT Widgets
- C++Qt开发——SMTP协议
- Qt编写安防视频监控系统68-兼容Qt4到Qt6
- 【Qt】在ubuntu上安装qt的mqtt模块
- QT 开源类库集合
- Qt之标准对话框(文件对话框)
- C++/Qt:TXT文件读写
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
- Qt creator 编译错误 :cannot find file .pro qt
- Qt学习一门:直接使用QT具
- QT creator使用(四):布局管理系统
- Qt编写可视化大屏电子看板系统32-模块10大屏地图
- Qt编写可视化大屏电子看板系统5-恢复布局
- Qt编写可视化大屏电子看板系统4-布局另存
- Qt编写控件属性设计器1-加载插件