当前位置:网站首页>QT notes - EventFilter event filter
QT notes - EventFilter event filter
2022-07-24 12:04:00 【Cool breeze in the old street °】
Qt The framework provides us with a series of event processing mechanisms , When a window event occurs , The event will go through : The incident was distributed ->
Event filtering -> Event distribution -> Several stages of event handling .
every last Qt Each application corresponds to a unique QApplication Application object , Then call the object's exec() function , such Qt
The event detection inside the framework starts ( The program will enter an event loop to listen for the application's events ).
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Event distribution process :
When the event occurs ,Qt Call with application object notify() Function to send the event to the specified window :
[override virtual] bool QApplication::notify(QObject *receiver, QEvent *e);
Events can be filtered through the event filter during sending , By default, any generated events are not filtered :
// You need to install a filter on the window first , This event will trigger
void QObject::installEventFilter(QObject *filterObj) // Event filter
[virtual] bool QObject::eventFilter(QObject *watched, QEvent *event)
Help document :
When the event is sent to the specified window , The event distributor of the window will classify the received events :
[override virtual protected] bool QWidget::event(QEvent *event);
Help document :
The event dispatcher will classify the events after classification ( Mouse events 、 Keyboard events 、 Graphic Events ...) Distribute to the corresponding event handler function for processing , Each event handler function has a default processing action ( We can also rewrite these event handler functions ), such as : Mouse events :
// The mouse click
[virtual protected] void QWidget::mousePressEvent(QMouseEvent *event);
// Mouse release
[virtual protected] void QWidget::mouseReleaseEvent(QMouseEvent *event);
// Mouse movement
[virtual protected] void QWidget::mouseMoveEvent(QMouseEvent *event);
An example of an event filter is as follows :
If an event filter is installed on the component , that , Filters can capture events before components , So as to carry out corresponding treatment .
First step : Define our event filter events
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
The second step : Bind the object to be bound to the event filter
ui.label->installEventFilter(this);
ui.label_move->installEventFilter(this);
The third step : Rewrite our event filter event
bool QtEventFilter::eventFilter(QObject* watched, QEvent* event)
{
// Objects to filter
if (watched == ui.label)
{
// Events to be filtered
if (event->type() == QEvent::MouseMove)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
QPoint p = mouseEvent->pos();
QString s = QString("x=%1,y=%2").arg(QString::number(p.x())).arg(QString::number(p.y()));
ui.lineEdit_move->setText(s);
}
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mousePressEvent = (QMouseEvent*)event;
if (mousePressEvent->buttons() & Qt::LeftButton)
{
QPoint p1 = mousePressEvent->pos();
QString s1 = QString("x=%1,y=%2").arg(QString::number(p1.x())).arg(QString::number(p1.y()));
ui.lineEdit_left->setText(s1);
}
}
if (event->type() == QEvent::MouseButtonDblClick)
{
qDebug() << QStringLiteral("label Use MouseButtonDblClick");
}
}
if (ui.label_move == watched)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QStringLiteral(" This is the filter MouseMove");
// Intercept Don't continue to pass down
return true;
}
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QStringLiteral(" This is the filter MouseButtonPress ");
// Don't intercept , Keep delivering
return false;
}
}
// Pass other events to the base class
return QWidget::eventFilter(watched, event);
}
MyLabel.cpp Customize Label Mouse press and mouse move events
#include "MyLabel.h"
MyLabel::MyLabel(QWidget *parent)
: QLabel(parent)
{
this->setMouseTracking(true); // Set mouse tracking
}
MyLabel::~MyLabel()
{
}
void MyLabel::mousePressEvent(QMouseEvent *e)
{
qDebug() << QStringLiteral(" This is the subcomponent :mousePressEvent");
if (e->buttons() == Qt::LeftButton)
{
qDebug() << QStringLiteral(" Left mouse button ");
}
else if(e->buttons() == Qt::RightButton)
{
qDebug() << QStringLiteral(" Right click the mouse ");
}
e->ignore();
}
void MyLabel::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << QStringLiteral(" This is the subcomponent :mouseMoveEvent x=%1,y=%2").arg(e->x()).arg(e->y());
e->ignore();
}
MyLabel.h
#pragma once
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent);
~MyLabel();
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
};
Main window code :
QtEventFilter.cpp
#include "QtEventFilter.h"
#include <QDebug>
QtEventFilter::QtEventFilter(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
// Install event filter
ui.label->installEventFilter(this);
ui.label_move->installEventFilter(this);
ui.label->setMouseTracking(true);
}
QtEventFilter::~QtEventFilter()
{
}
bool QtEventFilter::eventFilter(QObject* watched, QEvent* event)
{
if (watched == ui.label)
{
if (event->type() == QEvent::MouseMove)
{
//m_SerPort = Port_Screen;
QMouseEvent* mouseEvent = (QMouseEvent*)event;
QPoint p = mouseEvent->pos();
QString s = QString("x=%1,y=%2").arg(QString::number(p.x())).arg(QString::number(p.y()));
ui.lineEdit_move->setText(s);
}
if (event->type() == QEvent::MouseButtonPress)
{
//m_SerPort = Port_Left;
QMouseEvent* mousePressEvent = (QMouseEvent*)event;
if (mousePressEvent->buttons() & Qt::LeftButton)
{
QPoint p1 = mousePressEvent->pos();
QString s1 = QString("x=%1,y=%2").arg(QString::number(p1.x())).arg(QString::number(p1.y()));
ui.lineEdit_left->setText(s1);
}
}
if (event->type() == QEvent::MouseButtonDblClick)
{
qDebug() << QStringLiteral("label Use MouseButtonDblClick");
}
}
if (ui.label_move == watched)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QStringLiteral(" This is the filter MouseMove");
// Intercept
return true;
}
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QStringLiteral(" This is the filter MouseButtonPress ");
// Don't intercept , Keep delivering
return false;
}
}
// Pass on other events
return QWidget::eventFilter(watched, event);
}
void QtEventFilter::mouseDoubleClickEvent(QMouseEvent* event)
{
if (event->type() == QEvent::MouseButtonDblClick)
{
qDebug() << "QEvent::MouseButtonDblClick";
}
}
void QtEventFilter::mousePressEvent(QMouseEvent* event)
{
if (event->type() == QEvent::MouseButtonPress)
{
qDebug() << QStringLiteral(" This is the parent component QEvent::MouseButtonPress");
}
}
void QtEventFilter::mouseMoveEvent(QMouseEvent* event)
{
if (event->type() == QEvent::MouseMove)
{
qDebug() << QStringLiteral(" This is the parent component QEvent::MouseMove");
}
}
QtEventFilter.h
#include <QtWidgets/QWidget>
#include "ui_QtEventFilter.h"
#include <QRect>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>
class QtEventFilter : public QWidget
{
Q_OBJECT
public:
QtEventFilter(QWidget *parent = Q_NULLPTR);
~QtEventFilter();
protected:
bool eventFilter(QObject* watched, QEvent* event);
void mouseDoubleClickEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
private:
Ui::QtEventFilterClass ui;
};
Let's look at the event filter 
true: The ui.lable_move Object's mouseMove The event was intercepted , Do not pass down execution MyLabel Rewrite the mousemove event
false: The ui.lable_move Object's mousepress The event is not intercepted , Continue to pass down execution MyLabel Rewrite the mousepress event
Pictured :
It is proved that the event filter filters MyLabel Rewrite the mousemove event . If there's no filtering , prints “ This is the subcomponent :mouseMoveEvent x=%1,y=%2”.
When the sub component receives mousepress When an event is :
e->ignore();// Ignore this event , Continue to pass on ( Passed to its parent class )
e->accept();// Accept this event , Don't continue to pass down ( The next object passed is its parent , Is not the base class )

Proved : After the sub component accepts the event , If four e->ignore The corresponding event that will continue to be passed to the parent component
There is no effect on other events except the filtered events , Because the event dispatcher function of the parent class is called at the end of the rewritten event dispatcher function
// The event dispatcher function of the parent class , Other events are distributed according to the default distribution process
return QWidget::eventFilter(watched, event);
This ensures that other events are distributed according to the default distribution process , And was eventually disposed of by the window .
ui chart :
Reference blog :
https://subingwen.cn/qt/event_handler/
https://blog.csdn.net/fengjliu/article/details/48572089
边栏推荐
- MOS tube - Notes on rapid recovery application (I) [principle]
- Dynamic memory management
- 6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
- Blue team resource collection
- 利用huggingface模型翻译英文
- 生信周刊第37期
- 安装jmeter
- Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
- 【我也想刷穿 LeetCode啊】468. 验证IP地址
- 1184. 公交站间的距离 : 简单模拟题
猜你喜欢
随机推荐
哈希——202. 快乐数
Top and bottom of stack
Browser logic vulnerability collection
Three small knowledge points about data product managers
Makefile quick use
[Commons beanautils topic] 005- convertutils topic
Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation
Svn server and client installation (Chinese package) and simple use
CCF 201803_ 1 jump jump
Hash - 202. Happy number
PM's alarm: "NPM warn config global --global, --local are deprecated
字符串——344.反转字符串
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
Use and expansion of fault tolerance and fusing
L1-043 阅览室
理解数据的存与取
Nacos permissions and databases
链表——142. 环形链表 II
第0章 前言和环境配置
Use of multithreading in QT






![Operational amplifier - Notes on rapid recovery [II] (application)](/img/fd/e12f43e23e6ec76c2b44ce7813e204.png)

