Skip to content

Commit

Permalink
增加 事件过滤器
Browse files Browse the repository at this point in the history
  • Loading branch information
nekosilverfox committed Dec 4, 2022
1 parent b8c80b9 commit 9e78045
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
1 change: 0 additions & 1 deletion 025_Qt_Event/mylabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ bool MyLabel::event(QEvent* event)
return QLabel::event(event); // 其他事件,让父亲做默认处理
}


1 change: 1 addition & 0 deletions 025_Qt_Event/mylabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MyLabel : public QLabel
/* 事件拦截 */
virtual bool event(QEvent* event);


signals:


Expand Down
22 changes: 22 additions & 0 deletions 025_Qt_Event/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_widget.h"
#include <QTimerEvent>
#include <QTimer>
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
: QWidget(parent)
Expand All @@ -26,6 +27,9 @@ Widget::Widget(QWidget *parent)
connect(ui->btnStop, &QPushButton::clicked,
this, [=](){timer3->stop();});

/* 对 label 安装事件过滤器*/
ui->label->installEventFilter(this);


}

Expand All @@ -49,3 +53,21 @@ void Widget::timerEvent(QTimerEvent* event)
ui->label_3->setText(QString::number(num2++));
}
}


/* 事件过滤器 */
bool Widget::eventFilter(QObject *object, QEvent *event)
{
/* 过滤在 `label` 上的`点击`事件 */
if (object == ui->label)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEv = static_cast<QMouseEvent*>(event); // 【复习】将父类转换为子类
qDebug() << "事件过滤器::鼠标[按下]了 - " << QString("鼠标当前位置 x: %1, y: %2").arg(mouseEv->x()).arg(mouseEv->y());
return true; // 返回 true 代表拦截
}
}

return QWidget::eventFilter(object, event); // 其他的事件,交给父类处理
}
4 changes: 4 additions & 0 deletions 025_Qt_Event/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Widget : public QWidget
/* 定时器事件 */
void timerEvent(QTimerEvent* event);


/* 事件过滤器 */
virtual bool eventFilter(QObject *object, QEvent *event);

private:
Ui::Widget *ui;

Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,43 @@ bool MyLabel::event(QEvent* event)
}
```



## 事件过滤器

其实 App 和 Event 之间还可以再进行一层拦截,也就是**事件过滤器**

![image-20221204171416935](doc/pic/README/image-20221204171416935.png)

**使用步骤:**

1. 那个控件需要事件过滤器就给它安装事件过滤器

```c++
ui->label->installEventFilter(this);
```



2. 重写 `eventFilter` 事件

```c++
/* 事件过滤器 */
bool Widget::eventFilter(QObject *object, QEvent *event)
{
/* 过滤在 `label` 上的`点击`事件 */
if (object == ui->label)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEv = static_cast<QMouseEvent*>(event); // 【复习】将父类转换为子类
qDebug() << "事件过滤器::鼠标[按下]了 - " << QString("鼠标当前位置 x: %1, y: %2").arg(mouseEv->x()).arg(mouseEv->y());
return true; // 返回 true 代表拦截
}
}

return QWidget::eventFilter(object, event); // 其他的事件,交给父类处理
}
```


Binary file added doc/pic/README/image-20221204171416935.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9e78045

Please sign in to comment.