当前位置:网站首页>Event filter
Event filter
2022-07-27 05:02:00 【PHP code】
occasionally , Objects need to be viewed 、 Even to intercept events sent to another object . for example , The dialog may want to block key events , Don't let other components receive ; Or to change the default handling of the Enter key .
Through the previous chapters , We already know ,Qt Created QEvent After the event object , Would call QObject Of event() Function to handle the distribution of events . obviously , We can do it in event() Function to achieve the operation of interception . because event() The function is protected Of , therefore , You need to inherit an existing class . If there are many components , You need to rewrite a lot of them event() function . It's quite troublesome, of course , Not to mention rewriting event() Functions have to be careful with a lot of problems . Fortunately Qt Provides another mechanism to achieve this : Event filter .
QObject There is one eventFilter() function , Used to build event filters . The signature of this function is as follows :
virtual bool QObject::eventFilter ( QObject * watched, QEvent * event );
This function, as its name suggests , It's a “ Event filter ”. So called event filters , It can be understood as a kind of filtering code . Think about the filters used in chemical experiments , Impurities can be left on the filter paper , Let the filtered liquid slip away . The same is true of event filters : It will check the received events . If this is the type of event we're interested in , Just do our own thing ; If not , Just keep forwarding . This function returns one bool type , If you want to put the parameter event To filter out , such as , Don't want it to continue forwarding , Just go back to true, Otherwise return to false. The call time of the event filter is the target object ( That's what's in the parameter watched object ) Before receiving the event object . in other words , If you stop an event in the event filter , that ,watched Object and all subsequent event filters will not know such an event at all .
Let's look at a simple piece of code :
class MainWindow : public QMainWindow
{
public:
MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
QTextEdit *textEdit;
};
MainWindow::MainWindow()
{
textEdit = new QTextEdit;
setCentralWidget(textEdit);
textEdit->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == textEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
MainWindow It's a class that we define . We rewrote it eventFilter() function . To filter events on specific components , First of all, we need to determine whether the object is a component that we are interested in , And then determine the type of event . In the code above , We don't want textEdit Component handles keyboard pressed Events . therefore , First we find this component , If this event is a keyboard event , Then return directly true, Which is to filter out this event , Other events still need to be dealt with , So back false. For other components , We don't guarantee that there's a filter , So the safest way is to call the function of the parent class .
eventFilter() Function is equivalent to creating a filter , Then we need to install this filter . To install the filter, you need to call QObject::installEventFilter() function . The signature of this function is as follows :
void QObject::installEventFilter ( QObject * filterObj )
This function accepts a QObject * Parameters of type . Remember what we just said ,eventFilter() The function is QObject A member function of , therefore , arbitrarily QObject Can be used as event filters ( The problem lies in , If you don't rewrite eventFilter() function , This event filter has no effect , Because by default nothing will be filtered ). The existing filter can pass through QObject::removeEventFilter() Function removal .
We can install multiple event handlers on one object , Just call it many times installEventFilter() function . If an object has multiple event filters , that , The last one to install will be the first to execute , That is, the sequence of last in first execution .
Remember our previous example ? We use event() Function handles Tab key :
bool CustomWidget::event(QEvent *e)
{
if (e->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
if (keyEvent->key() == Qt::Key_Tab) {
qDebug() << "You press tab.";
return true;
}
}
return QWidget::event(e);
}
Now? , We can give you a version that uses event filters :
bool FilterObject::eventFilter(QObject *object, QEvent *event)
{
if (object == target && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Tab) {
qDebug() << "You press tab.";
return true;
} else {
return false;
}
}
return false;
}
The power of event filters is , We can add an event filter for the entire application . Remember ,installEventFilter() The function is QObject Function of ,QApplication perhaps QCoreApplication Objects are QObject Subclasses of , therefore , We can approach QApplication perhaps QCoreApplication Add event filters . This global event filter will be called before any event filter of all other feature objects . Powerful as it is , But this behavior will seriously reduce the efficiency of event distribution of the whole application . therefore , Unless it has to be used , Otherwise, we shouldn't do it .
Be careful , If you are in the event filter delete A receiving component , Be sure to set the return value of the function to true. otherwise ,Qt The event will still be distributed to the receiving component , This causes the program to crash .
The event filter and the component on which the filter is installed must be on the same thread , otherwise , The filter will not work . in addition , If after installing the filter , These two components go to different threads , that , The filter will only work when they return to the same thread .
边栏推荐
猜你喜欢

Hiding skills of Photoshop clipping tool

Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?

What about PS too laggy? A few steps to help you solve the problem

【C语言】动态内存管理

项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知

Final Cut Pro中文教程 (2) 素材窗口的认识

Read write separation and master-slave synchronization

Explanation of index failure principle and its common situations

C language - two dimensional array, pointer

Plato farm is expected to further expand its ecosystem through elephant swap
随机推荐
项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
使用mq消息队列来进行下单流程的高并发设计,消息挤压,消息丢失,消息重复的产生场景和解决方案
对话框数据传递
Explanation of index failure principle and its common situations
Review of various historical versions of Photoshop and system requirements
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
Invert a Binary Tree
Be diligent in talking about what sidelines you can do now
kali系统arp介绍(断网嗅探密码抓包)
「Photoshop2021入门教程」“拉平”带有透视感的图像
Error: cannot read properties of undefined (reading 'then')
Why is count (*) slow
报错:cannot read poperties of undefined(reading ‘then‘)
使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
Easily download data in power Bi reports with power auto
接口和抽象类/方法学习 demo
Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images
【牛客讨论区】第七章:构建安全高效的企业服务
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
标准对话框 QMessageBox