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

边栏推荐
- National vocational college skills competition (secondary vocational group) network security competition questions - Analysis
- [matlab] matlab simulation modulation system FM system
- Just do it with your hands 7 - * project construction details 2 - hook configuration
- 如何使用postman实现简单的接口关联【增删改查】
- [matlab] matlab simulation modulation system - VSB system
- [technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
- We believe that the development of consumer Internet will still be limited to the Internet industry itself
- cmake
- TCP状态转换图
- [matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
猜你喜欢

Zhongke panyun-d module analysis and scoring standard

2022危险化学品经营单位安全管理人员上岗证题库及答案

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

VSCode的有用插件

定制一个自己项目里需要的分页器

拓扑排序和关键路径的图形化显示

Character types of C language

Unity is connected to the weather system

中職組網絡安全—內存取證

抓包整理外篇fiddler———— 会话栏与过滤器
随机推荐
Headache delayed double deletion
[untitled]
Graduation design of small programs -- small programs of food and recipes
[paper summary] zero shot semantic segmentation
Nodejs learning document
EVM proof in appliedzkp zkevm (11)
加密和解密
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
The second case analysis of the breakthrough of defense system from the perspective of the red team
力扣 第 300 场周赛
如何构建属于自己的知识引擎?社群开放申请
appliedzkp的zkevm(12)State Proof
[matlab] matlab simulation modulation system SSB system
[matlab] communication signal modulation general function interpolation function
中職組網絡安全—內存取證
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
【MATLAB】MATLAB 仿真 — 低通高斯白噪声
2022 t elevator repair operation certificate examination question bank and simulation examination
【MATLAB】通信信号调制通用函数 — 插值函数
Detailed comparison of Hynix emmc5.0 and 5.1 series