Qt的事件过滤器installEventFilter

一、介绍

WPF中使用AddHandler来监听事件,那么QT呢?Qt的事件模型是使用一个QObject对象,来监视发送其他QObject对象的事件,在事件到达之前对其进行处理。这里要使用一个函数

void QObject::installEventFilter(QObject *filterObj)

Qt助手的解释如下:

在对象上安装一个事件过滤器filterObj。如下:

 monitoredObj->installEventFilter(filterObj);

其中monitoredObj、filterObj都是QObject的子类。上面代码意思是:在monitoredObj对象上安装一个事件过滤器filterObj。该函数一般和如下函数配合使用:

[virtual] bool QObject::eventFilter(QObject *watched, QEvent *event)

注意:该函数是虚函数,也就是说派生自QObject的子类可以重写该函数。

上面monitoredObj对象安装一个filterObj过滤器后,则可以在filterObj对象所在类的eventFilter函数

 class KeyPressEater : public QObject
  {
      Q_OBJECT
      ...
 
  protected:
      bool eventFilter(QObject *obj, QEvent *event) override;
  };
 
  bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
  {
      if (event->type() == QEvent::KeyPress) {
          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
          qDebug("Ate key press %d", keyEvent->key());
          return true;
      } else {
          // standard event processing
          return QObject::eventFilter(obj, event);
      }
  }

现在我们在按钮或QListView两个窗体部件上安装过滤器,如下:

      KeyPressEater *keyPressEater = new KeyPressEater(this);
      QPushButton *pushButton = new QPushButton(this);
      QListView *listView = new QListView(this);
     
      pushButton->installEventFilter(keyPressEater);
      listView->installEventFilter(keyPressEater);

二、实例

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QLabel>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QPixmap>
#include<QMouseEvent>
#include<QImage>
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
public:
    QLabel * label1;
    QLabel * label2;
    QLabel * label3;
    QLabel * labelstate;
    QImage image1;
    QImage image2;
    QImage image3;

private:
    Ui::Dialog *ui;
private slots:
    bool eventFilter(QObject *, QEvent *);
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    label1 = new QLabel;
    label2 = new QLabel;
    label3 = new QLabel;
    labelstate = new QLabel;
    labelstate->setAlignment(Qt::AlignHCenter);

    image1.load(":/BABY.jpg");
    image2.load(":/baby.jpg");
    image3.load(":/huanhuan.jpg");

    QMatrix matrix;
    matrix.scale(0.3, 0.3);
    image1 = image1.transformed(matrix);
    image2 = image2.transformed(matrix);
    image3 = image3.transformed(matrix);

    label1->setPixmap(QPixmap::fromImage(image1));
    label2->setPixmap(QPixmap::fromImage(image2));
    label3->setPixmap(QPixmap::fromImage(image3));

    QHBoxLayout * hor = new QHBoxLayout;
    hor->addWidget(label1);
    hor->addWidget(label2);
    hor->addWidget(label3);

    QVBoxLayout * ver = new QVBoxLayout;
    ver->addLayout(hor);
    ver->addWidget(labelstate);
    setLayout(ver);

    label1->installEventFilter(this);
    label2->installEventFilter(this);
    label3->installEventFilter(this);



}
bool Dialog::eventFilter(QObject *watched, QEvent *event)
{
    //判断当前发生事件的对象
    if (watched == label1)
    {
        if (event->type() == QEvent::MouseButtonPress) //判断发生的事件类型
        {
            //将事件event转化为鼠标事件
            QMouseEvent * mouseEvent = (QMouseEvent *)event;
            if (mouseEvent->button()&Qt::LeftButton)
            {
                labelstate->setText(tr("Left mouse button pressed on left image"));
            }
            if (mouseEvent->button()&Qt::RightButton)
            {
                labelstate->setText(tr("middle mouse button pressed on left image"));
            }
            if (mouseEvent->button()&Qt::MidButton)
            {
                labelstate->setText(tr("right mouse button pressed on left image"));
            }
            QMatrix matrix;
            matrix.scale(2, 2);
            QImage tmp1 = image1.transformed(matrix);
            label1->setPixmap(QPixmap::fromImage(tmp1));
        }
        if (event->type() == QEvent::MouseButtonRelease)
        {
            labelstate->setText(tr("Mouse button released from left image"));
            label1->setPixmap(QPixmap::fromImage(image1));

        }
    }
    if (watched == label2)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent * mouseEvent = (QMouseEvent *)event;
            if (mouseEvent->button()&Qt::LeftButton)
            {
                labelstate->setText(tr("Left mouse button pressed on middle image"));
            }
            if (mouseEvent->button()&Qt::RightButton)
            {
                labelstate->setText(tr("middle mouse button pressed on middle image"));
            }
            if (mouseEvent->button()&Qt::MidButton)
            {
                labelstate->setText(tr("right mouse button pressed on middle image"));
            }
            QMatrix matrix;
            matrix.scale(2, 2);
            QImage  tmp2 = image2.transformed(matrix);
            label2->setPixmap(QPixmap::fromImage(tmp2));
        }
        if (event->type() == QEvent::MouseButtonRelease)
        {
            labelstate->setText(tr("Mouse button released from middle image"));
            label2->setPixmap(QPixmap::fromImage(image2));
        }

    }
    if (watched == label3)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent * mouseEvent = (QMouseEvent *)event;
            if (mouseEvent->button()&Qt::LeftButton)
            {
                labelstate->setText(tr("Left mouse button pressed on right image"));
            }
            if (mouseEvent->button()&Qt::RightButton)
            {
                labelstate->setText(tr("middle mouse button pressed on right image"));
            }
            if (mouseEvent->button()&Qt::MidButton)
            {
                labelstate->setText(tr("right mouse button pressed on right image"));
            }
            QMatrix matrix;
            matrix.scale(2, 2);
            QImage  tmp3 = image3.transformed(matrix);
            label3->setPixmap(QPixmap::fromImage(tmp3));
        }
        if (event->type() == QEvent::MouseButtonRelease)
        {
            labelstate->setText(tr("Mouse button released from right image"));
            label3->setPixmap(QPixmap::fromImage(image3));
        }
    }
    //调用QDialog::eventFilter将事件交给上层对话框
    return QDialog::eventFilter(watched, event);

}


Dialog::~Dialog()
{
    delete ui;
}

参考:

installEventFilter、eventFilter函数理解_荆楚闲人的博客-CSDN博客_installeventfilter

 QT学习之一:安装事件过滤器(installEventFilter)_隨意的風的博客-CSDN博客

 

 

 

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值