QT—布局管理器之BoxLayout篇
1.布局管理器的概述
在Qt中,使用布局管理器的主要原因是它能够自动管理组件的大小和位置,从而实现灵活且动态的界面布局。布局管理器可以自动调整组件以适应窗口大小的变化,确保界面在不同分辨率和设备上都能保持良好的显示效果。这不仅减少了开发者手动调整组件位置和大小的工作量,还提高了开发效率。
此外,布局管理器支持多种布局方式,如水平布局、垂直布局、网格布局和表单布局等,能够满足不同的界面设计需求。开发者可以根据具体需求选择合适的布局管理器,快速构建出整洁、美观且功能强大的用户界面。
2.盒子布局(BoxLayout)的概述及其公有函数
2.1BoxLayout概述
QBoxLayout:可以在水平方向或垂直方向上排列控件,分别派生QHBoxLayout(水平布局)、QVBoxLayout(垂直布局)子类
2.2BoxLayout公有函数
在QT中选中要查找的布局,键盘上点击F1键即可查看使用手册
下面介绍几种常见的公有函数
- void addLayout(QLayout* layout,int stretch = 0) 将layout添加到框的末端,使用连续拉伸因子拉伸。
- void addSpacing(int size) 添加一个大小为size的不可伸缩空间(QSpacerItem)到这个框布局的末尾
- void addStretch(int stretch = 0) 添加一个可伸缩空间(一个QSpacerItem),最小尺寸为零,拉伸因子stretch到这个框布局的末尾。
- void addStrut(int size) 限制盒子的垂直尺寸最小为size
- void addWidget(QWidget* widget,int stretch = 0,Qt::Alignment alignment = 0) 将小部件添加到此框布局的末尾,并使用拉伸因子拉伸和对齐对齐。
- void setDirection(QBoxLayout::Direction direction) 设置此布局的方向为direction。
- void setSpacing(int spacing) 设置小部件之间的间距
- void setStretch(int index,int stretch) 给index位置的控件设置拉伸因子stretch
- void QLayout::setMargin(int margin) 设置布局管理器中所有控件的外边距,上、下、左、右外边距的大小都为 margin。默认情况下,所有方向的外边距为 11 px。
3.盒子布局的示例
以视频点播器的布局为例
效果图:
.h头文件
#ifndef MAINWIDGET_H
#define MAINWIDGET_H#include <QWidget>
#include <QBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QBoxLayout>
#include <QListWidget>
class MainWidget : public QWidget
{Q_OBJECTpublic:MainWidget(QWidget *parent = 0);~MainWidget();void initUI();QBoxLayout* hlayout;//总的布局QHBoxLayout* totalLayout;//左边和右边的布局QVBoxLayout* leftLayout,*rightLayout;//左右的widgetQWidget* leftW,*rightW;//左边有logo和频道表QLabel* logo;QListWidget* channelWin;//右边QWidget* topWin,*bannerWin,*videoWin;
};#endif // MAINWIDGET_H
.cpp的布局实现
//视频播放器的布局//总布局this->totalLayout=new QHBoxLayout;//左边的布局this->leftW=new QWidget();this->leftW->setStyleSheet("background:yellow");this->leftLayout=new QVBoxLayout;//左边布局的控件this->logo=new QLabel("logo",this);this->logo->setStyleSheet("background:red");this->channelWin=new QListWidget;this->channelWin->setStyleSheet("background:balck");//将左边布局的控件添加到左布局中this->leftLayout->addWidget(this->logo,1);this->leftLayout->addWidget(this->channelWin,5);//设置边距this->leftLayout->setSpacing(0);this->leftLayout->setMargin(0);//将左布局设置到左窗口中this->leftW->setLayout(this->leftLayout);//----------左边-----------------//右边的布局this->rightW=new QWidget();this->rightW->setStyleSheet("background:blue");this->rightLayout=new QVBoxLayout;//右边的控件this->topWin=new QWidget;this->topWin->setStyleSheet("background:green");this->bannerWin=new QWidget;this->bannerWin->setStyleSheet("background:pink");this->videoWin=new QWidget;this->videoWin->setStyleSheet("background:orange");//将控件添加到布局this->rightLayout->addWidget(this->topWin,2);this->rightLayout->addWidget(this->bannerWin,3);this->rightLayout->addWidget(this->videoWin,2);this->rightLayout->setSpacing(0);this->rightLayout->setMargin(0);this->rightW->setLayout(this->rightLayout);//总布局this->totalLayout->addWidget(this->leftW,2);this->totalLayout->addWidget(this->rightW,8);this->totalLayout->setSpacing(0);this->totalLayout->setMargin(0);this->setLayout(this->totalLayout);
4.总结
控件和布局的关系
控件(Widget):是用户界面的基本元素,需要被添加到布局管理器中。
布局管理器(Layout):用于管理控件的位置和大小,需要被设置给一个控件(通常是窗口或容器控件)。
关系:控件被添加到布局管理器中,布局管理器被设置给控件。
布局管理器的使用步骤总结
- 首先要先分析界面中的布局,例如视频播放器中分三个布局左右以及总布局
- 接着分析每个布局中的控件,如总布局可以分为左右俩边的QWidget
- 将控件添加到布局中,并将布局设置给控件。
- 利用setSpacing()和setMargin()设置间距