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

边栏推荐
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- Is there a handling charge for spot gold investment
- 不登陆或者登录解决oracle数据库账号被锁定。
- 给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
- LDC Build Shared Library
- Top ten regular spot trading platforms 2022
- Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
- 每日一题之干草堆的移动
- leetcode 6103 — 从树中删除边的最小分数
猜你喜欢

Basis of information entropy

【面试题】1369- 什么时候不能使用箭头函数?

【无标题】

JDBC courses

【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理

wirehark数据分析与取证A.pacapng

Using tensorboard to visualize the model, data and training process

Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

MySQL - database query - condition query
随机推荐
MySQL foundation 05 DML language
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
【C语言】指针与数组笔试题详解
Dotconnect for PostgreSQL data provider
leetcode 2097 — 合法重新排列数对
On Fibonacci sequence
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
Type expansion of non ts/js file modules
[Cao gongzatan] after working in goose factory for a year in 2021, some of my insights
[day 29] given an integer, please find its factor number
Embrace the safety concept of platform delivery
Matlab Doppler effect produces vibration signal and processing
Machine learning terminology
按键精灵打怪学习-自动回城路线的判断
数学知识:能被整除的数—容斥原理
The difference between tail -f, tail -f and tail
Matlab finds the position of a row or column in the matrix
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Arduino DY-SV17F自动语音播报