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

边栏推荐
- [system analyst's road] Chapter V double disk software engineering (development model development method)
- 一位苦逼程序员的找工作经历
- [Arduino experiment 17 L298N motor drive module]
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
- 异步、邮件、定时三大任务
- Meituan dynamic thread pool practice ideas, open source
- Trois tâches principales: asynchrone, courrier et timing
- 每日一题之干草堆的移动
- uniapp组件-uni-notice-bar通告栏
- [my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
猜你喜欢

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

MySQL --- 数据库查询 - 基本查询

MySQL basics 03 introduction to MySQL types

Basic remote connection tool xshell
![leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]](/img/2c/8ec3926243fac8db9ed45d8053f3af.png)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]

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

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

What is tone. Diao's story

Androd gradle's substitution of its use module dependency

leetcode 2097 — 合法重新排列数对
随机推荐
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Canvas drawing -- bingdd
Work experience of a hard pressed programmer
Draw love with go+ to express love to her beloved
Is there a handling charge for spot gold investment
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
JDBC courses
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
[androd] module dependency replacement of gradle's usage skills
Key wizard play strange learning - front desk and Intranet send background verification code
Detailed explanation of Q-learning examples of reinforcement learning
How wide does the dual inline for bread board need?
Type expansion of non ts/js file modules
机器学习术语
Expérience de recherche d'emploi d'un programmeur difficile
The meaning of wildcard, patsubst and notdir in makefile
Asynchronous, email and scheduled tasks
MySQL basics 03 introduction to MySQL types
【FH-GFSK】FH-GFSK信号分析与盲解调研究