網站首頁 編程語言 正文
1.事件過濾器
void QObject::installEventFilter(QObject *filterObj)
bool eventFilter(QObject *obj, QEvent *event);
Qt的事件過濾由以上兩個方法實現,首先安裝一個事件過濾器,然后重寫bool eventFilter(QObject *obj, QEvent *event)。
filterObj表示事件篩選器對象,它接收發送到此QObject對象的所有事件。篩選器可以停止事件,也可以將事件轉發給此QObject對象。事件過濾器filterObj通過它的eventFilter()函數接收事件。
eventFilter()有返回值。
- 如果返回true,表示事件過濾,不會發送到對象本身。
- 如果返回false,表示事件未過濾,會通過event()方法將事件分發到對象。
- 返回給基類進行處理,例:return QObject::eventFilter(obj, event)。
2.示例
一個label,當鼠標進入的時候變成紅色,鼠標離開的時候變為黑色
#include "widget.h"
#include "ui_widget.h"
QString redStyle = "QLabel#label{color:#FF0000}";
QString blackStyle = "QLabel#label{color:#000000}";
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label->installEventFilter(this);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if(obj == ui->label)
{
//鼠標進入的時候
if (event->type() == QEvent::Enter)
{
ui->label->setText("我是紅色");
ui->label->setStyleSheet(redStyle);
return true;
}
else if(event->type() == QEvent::Leave) //鼠標離開
{
ui->label->setText("我是黑色");
ui->label->setStyleSheet(blackStyle);
return true;
}
return false;//別的事件會傳給label對象
}
// standard event processing
return QWidget::eventFilter(obj, event);
}
上述代碼,假如我們不使用事件過濾器,我們就無法實現上述鼠標進入、離開功能,只能自己繼承QLabel,重寫鼠標進入、離開事件。
3.簡單分析
自定義一個label類繼承QLabel,查看事件的處理順序,以及過濾器是否起作用。
#ifndef WLABEL_H
#define WLABEL_H
#include <QLabel>
#include <QEvent>
class WLabel : public QLabel
{
public:
WLabel(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
protected:
virtual bool event(QEvent *e);
virtual void enterEvent(QEvent *event);
virtual void leaveEvent(QEvent *event);
};
#endif // WLABEL_H
#include "wlabel.h"
#include <QDebug>
WLabel::WLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent,f)
{
}
bool WLabel::event(QEvent *e)
{
if(e->type() == QEvent::Enter)
{
qDebug()<<"WLabel event :enter";
}
else if(e->type() == QEvent::Leave)
{
qDebug()<<"WLabel event :Leave";
}
return QLabel::event(e);
}
void WLabel::enterEvent(QEvent *event)
{
qDebug()<<"WLabel enterEvent";
}
void WLabel::leaveEvent(QEvent *event)
{
qDebug()<<"WLabel leaveEvent";
}
自己定義了一個WLabel類,重寫了event事件,鼠標進入事件enterEvent,鼠標離開事件leaveEvent。
然后將之前界面上的QLabel提升為我們現在定義的類。
如果在bool Widget::eventFilter(QObject *obj, QEvent *event)方法中,將我們鼠標進入離開事件返回false后,事件會發送到Label本身,如下圖所示。
結論:事件的派發順序是先進入eventFilter中,看是否過濾掉此事件,然后進入到bool WLabel::event(QEvent *e)事件中,由event去分發事件,最后進入到enterEvent(QEvent *event)或者void leaveEvent(QEvent *event);
原文鏈接:https://blog.csdn.net/wzz953200463/article/details/124258028
相關推薦
- 2022-07-16 Spring的隔離級別&事務傳播屬性&數據庫隔離級別之間的聯系
- 2024-01-16 解決 git pull 操作后文件權限變化
- 2022-07-03 如何讓Python在HTML中運行_python
- 2023-04-03 Python數據結構棧實現進制轉換簡單示例_python
- 2023-03-26 Kotlin?server多線程編程詳細講解_Android
- 2022-10-02 react+typescript中使用echarts的實現步驟_React
- 2022-06-23 C#使用DLLImport調用外部DLL的方法_C#教程
- 2022-05-14 linq中的分區操作符_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支