当前位置:网站首页>【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();
});
效果

边栏推荐
- 异步、邮件、定时三大任务
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- [principles of multithreading and high concurrency: 2. Solutions to cache consistency]
- Asynchronous, email and scheduled tasks
- Trois tâches principales: asynchrone, courrier et timing
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- MySQL foundation 05 DML language
- 一比特苦逼程序員的找工作經曆
- Look at how clothing enterprises take advantage of the epidemic
- Work experience of a hard pressed programmer
猜你喜欢

Basis of information entropy

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

异步、郵件、定時三大任務

Meituan dynamic thread pool practice ideas, open source

异步、邮件、定时三大任务

Top ten regular spot trading platforms 2022

Daily topic: movement of haystack

Expérience de recherche d'emploi d'un programmeur difficile

MySQL --- 数据库查询 - 条件查询

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
随机推荐
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
[Androd] Gradle 使用技巧之模块依赖替换
C#应用程序界面开发基础——窗体控制(3)——文件类控件
按键精灵打怪学习-前台和内网发送后台验证码
What are the trading forms of spot gold and what are the profitable advantages?
Basis of information entropy
[shutter] animation animation (shutter animation type | the core class of shutter animation)
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
Matlab Doppler effect produces vibration signal and processing
Assets, vulnerabilities, threats and events of the four elements of safe operation
Embrace the safety concept of platform delivery
MySQL
d,ldc构建共享库
Database SQL language 02 connection query
异步、邮件、定时三大任务
Thinkphp+redis realizes simple lottery
【第29天】给定一个整数,请你求出它的因子数
On Fibonacci sequence
Makefile中wildcard、patsubst、notdir的含义