当前位置:网站首页>【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,然后选中——提升;

边栏推荐
- Notepad++--显示相关的配置
- Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
- 【MATLAB】MATLAB 仿真模拟调制系统 — FM 系统
- [matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal
- Unity 接入天气系统
- [技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
- Exercise bubble sort
- Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
- 【MATLAB】通信信号调制通用函数 — 带通滤波器
- COMP1721 Creating Classes
猜你喜欢

模拟小根堆

Trie number dictionary tree

National vocational college skills competition (secondary vocational group) network security competition questions - Analysis

2022 Guangdong provincial competition - code information acquisition and analysis flag

如何使用postman实现简单的接口关联【增删改查】

空洞卷积、可变形卷积、可变形ROI Pooling

抓包整理外篇fiddler———— 会话栏与过滤器

KMP match string

LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703

Trie数-字典树
随机推荐
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
Exercise bubble sort
[paper summary] zero shot semantic segmentation
【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
[untitled]
定制一个自己项目里需要的分页器
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
[matlab] matlab simulation modulation system - DSB system
QT qtableview data column width adaptation
[matlab] general function of communication signal modulation Fourier transform
【MATLAB】MATLAB 仿真数字基带传输系统 — 数字基带传输系统
[matlab] communication signal modulation general function - low pass filter
Simulink与Arduino串口通信
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
[matlab] matlab simulation - narrow band Gaussian white noise
【MATLAB】MATLAB 仿真数字带通传输系统 — QPSK 和 OQPSK 系统
中科磐云—D模块解析以及评分标准
Zkevm (12) state proof of appliedzkp
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
Annex II: confidentiality agreement for offensive and defensive drills docx