一、介绍
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博客
文章来源地址https://www.toymoban.com/news/detail-521336.html
文章来源:https://www.toymoban.com/news/detail-521336.html
到了这里,关于Qt的事件过滤器installEventFilter的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!