当前位置:网站首页>【QT】制作MyComboBox点击事件
【QT】制作MyComboBox点击事件
2022-07-04 04:56:00 【米杰的声音】
在QT中的ui界面添加QComboBox控件,需求是实现控件的点击事件,查了资料,发现这个控件类的本身是没有点击信号可以使用的。制作ComboBox点击事件可分为三步:
1、重写ComboBox类;
2、在widget中添加对应启动的信号和槽事件;
3、在ui窗口中将ComboBox控件提升到重写的类里;
mycombobox.h
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
#include <QMouseEvent>
class MyComboBox : public QComboBox
{
Q_OBJECT//只有加入Q_OBJECT,才能使用信号signal和槽slot的机制
public:
explicit MyComboBox(QWidget *parent = 0);
~MyComboBox();
protected:
virtual void mousePressEvent(QMouseEvent *e);//添加鼠标点击事件
signals:
void clicked();//自定义点击信号,在mousePressEvent事件发生时触发
};
#endif // MYCOMBOBOX_Hmycombobox.c
#include "mycombobox.h"
MyComboBox::MyComboBox(QWidget *parent) : QComboBox(parent)
{
}
MyComboBox::~MyComboBox()
{
}
//重写mousePressEvent事件,检测事件类型是不是点击了鼠标左键
void MyComboBox::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
emit clicked(); //触发clicked信号
}
QComboBox::mousePressEvent(e); //将该事件传给父类处理,这句话很重要,如果没有,父类无法处理本来的点击事件
}
mainwindow.h
public slots:
void ComboBoxClicked();//添加槽函数mainwindow.c
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//添加链接,当点击发送到ComboBoxClicked()
connect(ui->cbb_pztWorkModel, SIGNAL(clicked()), this, SLOT(ComboBoxClicked()));
}
void MainWindow::ComboBoxClicked() {
ui->statusbar->showMessage("请输入正确的偶数个Hex字符(Hex字符仅包含0~9、A~F、空格)", 2000);
}
关于lineEdit控件的提升步骤为,点击进入窗口ui设计界面——选中ComboBox控件——右键——提升为——在提升类名称里添加刚刚重写的类MyComboBox,然后选中——提升;

边栏推荐
- Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
- Annex I: power of attorney for 202x XXX attack and defense drill
- Several smart watch related chips Bluetooth chip low power consumption
- 定制一个自己项目里需要的分页器
- appliedzkp zkevm(13)中的Public Inputs
- Nodejs learning document
- TCP状态转换图
- 中科磐云—数据分析与取证数据包flag
- 【MATLAB】MATLAB 仿真数字基带传输系统 — 数字基带传输系统
- 关于solidworks standard无法获得许可 8544问题的总结
猜你喜欢

如何构建属于自己的知识引擎?社群开放申请

Daily question brushing record (12)

2022 Guangdong provincial competition - code information acquisition and analysis flag

How to build your own knowledge engine? Community open application

Secondary vocational group network security - memory Forensics

KMP匹配字符串

Simple g++ and GDB debugging

Share some of my telecommuting experience

《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
![[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术](/img/87/e0469e280365ed0261e2b551ebd888.png)
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
随机推荐
We believe that the development of consumer Internet will still be limited to the Internet industry itself
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
flink1.13 sql基础语法(二)join操作
TCP状态转换图
2022g2 power station boiler stoker special operation certificate examination question bank and answers
Capturing and sorting out external Fiddler -- Conversation bar and filter
【MATLAB】通信信号调制通用函数 — 带通滤波器
【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(第 I 类部分响应波形)的眼图
《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it
中職組網絡安全—內存取證
Exercise bubble sort
基于单片机的太阳能杀虫系统
Character types of C language
Li Kou's 300th weekly match
中职组网络安全—内存取证
appliedzkp zkevm(11)中的EVM Proof
中科磐云—D模块解析以及评分标准
Secondary vocational group network security - memory Forensics
Network equipment emergency response Guide