Qt 总结
废弃
Qt
qt不跟随系统主题色 qputenv(“QT_QPA_PLATFORM”, “windows:darkmode=0”);
相关模块
Qt 布局设计常用
布局可以理解为带有一定方向对齐属性的html的div 常用属性 addWidget,addLayout,addSpacing 间距
水平布局 QHBoxLayout 和 QVBoxLayout(垂直布局)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
level::level(QWidget *parent) : QWidget(parent) {
this->setWindowTitle("水平布局");
this->setFixedSize(800, 600);
auto *level = new QHBoxLayout(this);
auto *path = new QLabel(this);
path->setObjectName("path");
path->setText("path");
path->setFixedSize(40, 50);
auto *edit = new QLineEdit(this);
edit->setObjectName("edit");
edit->setFixedSize(100, 50);
auto *button = new QPushButton(this);
button->setObjectName("button");
button->setText("button1");
level->setAlignment(Qt::AlignLeft); //向左对齐
level->addWidget(path);
level->addWidget(button);
level->addWidget(edit);
auto *vertical = new QVBoxLayout(this);
auto *button1 = new QPushButton("按钮 1");
auto *button2 = new QPushButton("按钮 2");
auto *button3 = new QPushButton("按钮 3");
vertical->addWidget(button1);
vertical->addWidget(button2);
vertical->addWidget(button3);
level->addLayout(vertical); //添加垂直
}
QGridLayout(网格布局)
类似于表格的排列 addWidget(button1, 0, 0); // 放置在第一行第一列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fence::fence(QWidget *parent) :
QWidget(parent) {
auto* layout = new QGridLayout(this);
auto *button1 = new QPushButton("按钮 1");
auto *button2 = new QPushButton("按钮 2");
auto *vertical = new QVBoxLayout(this);
auto *b1 = new QPushButton("按钮 1");
auto *b2 = new QPushButton("按钮 2");
auto *b3 = new QPushButton("按钮 3");
vertical->addWidget(b1);
vertical->addWidget(b2);
vertical->addWidget(b3);
layout->addWidget(button1,0,1);
layout->addWidget(button2,1,1);
layout->addLayout(vertical,0,0);
}
QStackedLayout(堆叠布局)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
this->setWindowTitle("堆栈布局");
this->setFixedSize(600, 400);
//切换按钮
auto *pButton = new QPushButton("点击切换", this);
//切换的页面
auto *pFirstPage = new QLabel(this);
pFirstPage->setStyleSheet("QLabel{background-color:rgb(255, 0, 0)}");
auto *pSecondPage = new QLabel(this);
pSecondPage->setStyleSheet("QLabel{background-color:rgb(255, 255, 0)}");
auto *pThirdPage = new QLabel(this);
pThirdPage->setStyleSheet("QLabel{background-color:rgb(255, 0, 255)}");
// 添加页面(用于切换)
auto *m_pStackedLayout = new QStackedLayout();
m_pStackedLayout->addWidget(pFirstPage);
m_pStackedLayout->addWidget(pSecondPage);
m_pStackedLayout->addWidget(pThirdPage);
//绑定事件 槽
connect(pButton, &QPushButton::clicked, this, [=]() {
int nextPage = (m_pStackedLayout->currentIndex() + 1) % m_pStackedLayout->count();
m_pStackedLayout->setCurrentIndex(nextPage);
});
//垂直布局
auto *pLayout = new QVBoxLayout(this);
pLayout->addWidget(pButton, 0, Qt::AlignLeft | Qt::AlignVCenter);
pLayout->addLayout(m_pStackedLayout);
pLayout->setSpacing(10);
pLayout->setContentsMargins(10, 10, 10, 10);
QFormLayout(表单布局)
1
2
3
4
5
6
7
8
9
10
11
QFormLayout *formLayout = new QFormLayout(&window);
QLabel *nameLabel = new QLabel("姓名:");
QLineEdit *nameLineEdit = new QLineEdit();
QLabel *ageLabel = new QLabel("年龄:");
QLineEdit *ageLineEdit = new QLineEdit();
formLayout->addRow(nameLabel, nameLineEdit);
formLayout->addRow(ageLabel, ageLineEdit);
QSplitter(分裂器布局)不算布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
this->setWindowTitle("分裂器");
//水平布局
auto *layout = new QHBoxLayout(this);
auto *splitter = new QSplitter(this);
auto *widget = new QWidget(this);
widget->setWindowIconText("aa");
widget->setStyleSheet("background-color: rgb(25, 156, 0);");
widget->setMinimumSize(QSize(300, 300));//固定最小宽高
splitter->addWidget(widget);
//父级关联 不关联不发拖动
auto* splitter2 = new QSplitter(Qt::Vertical,splitter);
//鼠标弹起后在分割
splitter2->setOpaqueResize(false);
auto *widget2 = new QWidget(this);
widget2->setWindowIconText("bb");
widget2->setStyleSheet("background-color: rgb(255, 156, 0);");
widget2->setMinimumSize(QSize(300, 300));
auto* text = new QTextBrowser(this);
text->setReadOnly(false);//只读或者可编辑
splitter2->addWidget(widget2);
splitter2->addWidget(text);
splitter->addWidget(splitter2);
//添加分裂器
layout->addWidget(splitter);
QGroupBox(分组框布局)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
this->setWindowTitle("分组框布局");
// 主 :创建一个垂直布局
auto *layout = new QVBoxLayout(this);
// 创建一个QGroupBox
auto *groupBox = new QGroupBox("选项组", this);
groupBox->setCheckable(true); // 设置QGroupBox为可选中
// 在QGroupBox中添加控件
auto *checkBox1 = new QCheckBox("选项1", groupBox);
auto *checkBox2 = new QCheckBox("选项2", groupBox);
auto *radioButton1 = new QRadioButton("单选1", groupBox);
auto *radioButton2 = new QRadioButton("单选2", groupBox);
auto *lineEdit = new QLineEdit(groupBox);
// 内:创建一个水平布局来组织QGroupBox内的控件
auto *groupBoxLayout = new QHBoxLayout(groupBox);
groupBoxLayout->addWidget(checkBox1);
groupBoxLayout->addWidget(checkBox2);
groupBoxLayout->addWidget(radioButton1);
groupBoxLayout->addWidget(radioButton2);
groupBoxLayout->addWidget(lineEdit);
// 创建第二个个QGroupBox
auto *groupBox2 = new QGroupBox("第二组", this);
auto *checkBox5 = new QCheckBox("选项1", groupBox2);
// 内:创建一个水平布局来组织QGroupBox内的控件
auto *groupBoxLayout2 = new QHBoxLayout(groupBox2);
groupBoxLayout2->addWidget(checkBox5);
// 将QGroupBox添加到主窗口的布局中
layout->addWidget(groupBox);
layout->addWidget(groupBox2);
qt 类
qt 窗口类
QMainWindow : 包含菜单栏、工具栏、状态栏 QWidget:空窗口 常用 QDialog: 对话框,登录,弹窗,设置
Qt 常用库说明
Qt 信号与槽
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection) connect(Button, &QPushButton::clicked, this, &槽函数方法);// 或者lambda {}
Qt network
Qt
Reference
本文由作者按照 CC BY 4.0 进行授权