当前位置:网站首页>【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();
});
效果
边栏推荐
- [Androd] Gradle 使用技巧之模块依赖替换
- [自我管理]时间、精力与习惯管理
- 2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
- [Arduino experiment 17 L298N motor drive module]
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- 數學知識:臺階-Nim遊戲—博弈論
- Meituan dynamic thread pool practice ideas, open source
- Top ten regular spot trading platforms 2022
- Arduino dy-sv17f automatic voice broadcast
- The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
猜你喜欢
Daily topic: movement of haystack
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
MySQL
机器学习术语
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
Embrace the safety concept of platform delivery
异步、郵件、定時三大任務
Niu Ke swipes questions and clocks in
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
随机推荐
Daily topic: movement of haystack
2022 cable crane driver examination registration and cable crane driver certificate examination
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Create your first Kivy program Hello word (tutorial includes source code)
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
JS inheritance and prototype chain
机器学习术语
MySQL
The meaning of wildcard, patsubst and notdir in makefile
[C language] detailed explanation of pointer and array written test questions
MySQL foundation 04 MySQL architecture
Excel if formula determines whether the two columns are the same
How is the mask effect achieved in the LPL ban/pick selection stage?
Matlab Doppler effect produces vibration signal and processing
Kivy tutorial how to create drop-down lists in Kivy
Kivy教程大全之如何在 Kivy 中创建下拉列表
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées