当前位置:网站首页>【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 basic usage 02
- d,ldc构建共享库
- MySQL
- 力扣 204. 计数质数
- Daily topic: movement of haystack
- leetcode 6103 — 从树中删除边的最小分数
- How is the mask effect achieved in the LPL ban/pick selection stage?
- Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
- uniapp组件-uni-notice-bar通告栏
- [FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
猜你喜欢

MySQL - database query - condition query

一比特苦逼程序員的找工作經曆

Leetcode 2097 - Legal rearrangement of pairs

Top ten regular spot trading platforms 2022

MySQL basics 03 introduction to MySQL types

信息熵的基础
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

Niu Ke swipes questions and clocks in

Database SQL language 02 connection query

MySQL --- 数据库查询 - 基本查询
随机推荐
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
[self management] time, energy and habit management
力扣 204. 计数质数
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
Thinkphp+redis realizes simple lottery
Create your first Kivy program Hello word (tutorial includes source code)
Matlab finds the position of a row or column in the matrix
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
异步、郵件、定時三大任務
Leetcode 2097 - Legal rearrangement of pairs
MySQL foundation 06 DDL
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
[androd] module dependency replacement of gradle's usage skills
Key wizard hit strange learning - automatic path finding back to hit strange points
C#应用程序界面开发基础——窗体控制(3)——文件类控件
[system analyst's road] Chapter V double disk software engineering (development model development method)
Expérience de recherche d'emploi d'un programmeur difficile
How wide does the dual inline for bread board need?
Top ten regular spot trading platforms 2022
【面试题】1369- 什么时候不能使用箭头函数?