当前位置:网站首页>【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();
});
效果
边栏推荐
- 异步、邮件、定时三大任务
- 产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
- What is tone. Diao's story
- 异步、郵件、定時三大任務
- 测试右移:线上质量监控 ELK 实战
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- Druid database connection pool
- 【C语言】指针与数组笔试题详解
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- Why can't the start method be called repeatedly? But the run method can?
猜你喜欢
Basic concept and implementation of overcoming hash
Using tensorboard to visualize the model, data and training process
Asynchronous, email and scheduled tasks
Database SQL language 02 connection query
Basic remote connection tool xshell
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
[untitled]
Expérience de recherche d'emploi d'un programmeur difficile
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
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
随机推荐
强化学习 Q-learning 实例详解
Makefile中wildcard、patsubst、notdir的含义
How is the mask effect achieved in the LPL ban/pick selection stage?
MySQL foundation 07-dcl
Cut point of undirected graph
LDC Build Shared Library
leetcode:701. Insertion in binary search tree [BST insertion]
Canvas drawing -- bingdd
Top ten regular spot trading platforms 2022
信息熵的基础
异步、邮件、定时三大任务
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
Basic remote connection tool xshell
一位苦逼程序员的找工作经历
[untitled]
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
MySQL --- 数据库查询 - 条件查询
Trois tâches principales: asynchrone, courrier et timing
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
Daily topic: movement of haystack