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

边栏推荐
- Mathematical Knowledge: Steps - Nim Games - Game Theory
- [day 29] given an integer, please find its factor number
- MySQL - database query - basic query
- tail -f 、tail -F、tailf的区别
- Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
- 不登陆或者登录解决oracle数据库账号被锁定。
- [untitled]
- 看疫情之下服装企业如何顺势而为
- d,ldc构建共享库
- leetcode:701. Insertion in binary search tree [BST insertion]
猜你喜欢

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

Basic concept and implementation of overcoming hash

C#应用程序界面开发基础——窗体控制(3)——文件类控件

Niu Ke swipes questions and clocks in

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

软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053

Arduino dy-sv17f automatic voice broadcast

Matlab Doppler effect produces vibration signal and processing

Excel if formula determines whether the two columns are the same

MySQL --- 数据库查询 - 基本查询
随机推荐
对非ts/js文件模块进行类型扩充
Arduino dy-sv17f automatic voice broadcast
MySQL foundation 07-dcl
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
MySQL foundation 06 DDL
tp6快速安装使用MongoDB实现增删改查
JDBC courses
What is tone. Diao's story
异步、邮件、定时三大任务
按鍵精靈打怪學習-多線程後臺坐標識別
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
按键精灵打怪学习-回城买药加血
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
强化学习 Q-learning 实例详解
Draw love with go+ to express love to her beloved
MySQL
Excel removes the data after the decimal point and rounds the number
【FH-GFSK】FH-GFSK信号分析与盲解调研究
Asynchronous, email and scheduled tasks