QT布局
QLayout 类
QLayout 类是 Qt 中所有布局类的基类,用于管理窗口部件的布局。
布局类负责在窗口或容器中排列子部件,确保它们按照指定的规则进行排列和调整大小。
Qt 提供了几种常见的布局类,包括 QHBoxLayout、QVBoxLayout、QGridLayout 和 QFormLayout。
QObject
└── QLayoutItem
└── QLayout
├── QBoxLayout
│ ├── QHBoxLayout
│ └── QVBoxLayout
├── QGridLayout
└── QFormLayout
继承关系详解
QObject:
QObject 是 Qt 中所有可实例化类的基类,提供了信号和槽机制、对象树管理和运行时类型信息等功能。
QLayoutItem:
QLayoutItem 是一个抽象类,提供了布局项的基本接口,用于描述如何在布局中排列和调整大小。
QLayout:
QLayout 继承自 QObject 和 QLayoutItem,是所有布局类的基类。它提供了布局管理的基本功能,如添加和移除子部件、设置布局参数等。
QBoxLayout:
QBoxLayout 继承自 QLayout,是一个抽象类,提供了盒式布局的基本功能。盒式布局包括水平布局和垂直布局。
QHBoxLayout 和 QVBoxLayout 是 QBoxLayout 的具体实现类,分别用于水平和垂直布局。
QGridLayout:
QGridLayout 继承自 QLayout,提供了网格布局功能,可以将子部件按照行和列排列。
QFormLayout:
QFormLayout 继承自 QLayout,提供了表单布局功能,通常用于排列标签和输入框。
布局示例:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QDialog>
#include <QLabel>
#include <QComboBox>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
// 左侧布局
QLabel * UserNumber; // 用户编号
Q