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

边栏推荐
- appliedzkp zkevm(11)中的EVM Proof
- Nodejs learning document
- Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)
- 加密和解密
- 练习-冒泡排序
- [matlab] general function of communication signal modulation bandpass filter
- 2022危险化学品经营单位安全管理人员上岗证题库及答案
- Annex II: confidentiality agreement for offensive and defensive drills docx
- C basic (VII) document operation
- Notepad++--显示相关的配置
猜你喜欢

2022年A特种设备相关管理(电梯)考试题模拟考试平台操作

令人头痛的延时双删

Trie number dictionary tree

2022g2 power station boiler stoker special operation certificate examination question bank and answers

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

Share some of my telecommuting experience

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

数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技

Evolution of system architecture: differences and connections between SOA and microservice architecture
![[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre](/img/3e/b5df691ca1790469eb1b4e8ea5b4c0.png)
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
随机推荐
Automated testing selenium foundation -- webdriverapi
Character types of C language
VSCode的有用插件
KMP match string
【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
2022 t elevator repair operation certificate examination question bank and simulation examination
laravel 中获取刚刚插入的记录的id
appliedzkp的zkevm(12)State Proof
2022年T电梯修理操作证考试题库及模拟考试
2022 Guangdong provincial competition - code information acquisition and analysis flag
记几个智能手表相关芯片 蓝牙芯片 低功耗
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
We believe that the development of consumer Internet will still be limited to the Internet industry itself
cmake
Evolution of system architecture: differences and connections between SOA and microservice architecture
【MATLAB】MATLAB 仿真模拟调制系统 — DSB 系统
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
2022g2 power station boiler stoker special operation certificate examination question bank and answers
加密和解密