当前位置:网站首页>【QT】自定义控件的封装
【QT】自定义控件的封装
2022-07-03 01:04:00 【StudyWinter】
对QT自定义控件的封装。
1 创建工程,选择UI界面,继承Widget类。
2 创建设计师界面类,右键项目



类名自己取,到完成即可。最后会生成,h/.cpp/.ui文件
3 点击生成的UI文件

将需要的控件拖过来。这里使用Horizontal Slider和Spin Box。
最终效果就是拖动Horizontal Slider,Spin Box的数字会变;修改Spin Box的数字,Horizontal Slider会滑动。

还可以修改初始化,两个要对应

4 在ui界面里添加widget控件,将其提升为之前创建的设计师界面类

5 创建设计师界面类(名称自取)

6 使用信号的槽来完成需求;
6.1 首先设置【spinBox数字改变,是slider跟着滑动】
查看QSpinBox,它有一个重载的版本,使用函数指针来完成,

// spinBox数字改变,是slider跟着滑动
// 函数指针
void(QSpinBox::* spin)(int) = &QSpinBox::valueChanged;
connect(ui->spinBox, spin, ui->horizontalSlider,&QSlider::setValue);
接着,同理
// slider移动,spinBox数字跟着变化
connect(ui->horizontalSlider, &QSlider::valueChanged, ui->spinBox, &QSpinBox::setValue);6.2 在widget.ui添加两个按钮,【设置到一半】和【设置到一半】

在Smallwidget类中添加两个函数【设置数据】和【获取数据】

实现

在Widget.cpp中实现按钮的信号和槽
// 点击设置一半
connect(ui->btn_set, &QPushButton::clicked, this, [=](){
ui->widget->setVal(50);
});
// 点击获取当前值
connect(ui->btn_get, &QPushButton::clicked, this, [=](){
qDebug() << ui->widget->getVal();
});
效果

边栏推荐
- MySQL --- 数据库查询 - 条件查询
- 按鍵精靈打怪學習-多線程後臺坐標識別
- Look at how clothing enterprises take advantage of the epidemic
- Mongodb common commands of mongodb series
- JDBC courses
- Vim 9.0正式发布!新版脚本执行速度最高提升100倍
- 【面试题】1369- 什么时候不能使用箭头函数?
- [C language] detailed explanation of pointer and array written test questions
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- 基本远程连接工具Xshell
猜你喜欢

Telephone network problems

每日一题之干草堆的移动

Detailed explanation of Q-learning examples of reinforcement learning

【C语言】指针与数组笔试题详解

Draw love with go+ to express love to her beloved

C#应用程序界面开发基础——窗体控制(3)——文件类控件

攻克哈希的基本概念与实现

Using tensorboard to visualize the model, data and training process

Leetcode 6103 - minimum fraction to delete an edge from the tree

Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
随机推荐
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
Excel removes the data after the decimal point and rounds the number
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
【FH-GFSK】FH-GFSK信号分析与盲解调研究
MySQL foundation 07-dcl
Mongodb common commands of mongodb series
Matlab Doppler effect produces vibration signal and processing
按鍵精靈打怪學習-多線程後臺坐標識別
MySQL基础用法02
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
Button wizard play strange learning - automatic return to the city route judgment
Database SQL language 01 where condition
Assets, vulnerabilities, threats and events of the four elements of safe operation
[untitled]
按键精灵打怪学习-自动寻路回打怪点
JDBC courses
MySQL --- 数据库查询 - 条件查询
JS inheritance and prototype chain
LDC Build Shared Library
Excel if formula determines whether the two columns are the same