Qt中鼠标事件捕获与Qt对象事件过滤

        可以捕获鼠标事件或者Qt对象的事件,去做一些特定的操作。本文介绍Qt鼠标事件的捕获和Qt对象事件的监听捕获。

1、鼠标事件有:鼠标按下、弹起、双击、鼠标移动、鼠标滑轮上下移动。

上面事件对应的函数如下:

(1)鼠标按下:

[virtual protected] void QWidget::mousePressEvent(QMouseEvent *event)

(2)鼠标弹起:

[virtual protected] void QWidget::mouseReleaseEvent(QMouseEvent *event)

(3)双击事件:

[virtual protected] void QWidget::mouseDoubleClickEvent(QMouseEvent *event)

(4)鼠标移动事件:

[virtual protected] void QWidget::mouseMoveEvent(QMouseEvent *event)

(5)鼠标滑轮上下移动事件:

[virtual protected] void QWidget::wheelEvent(QWheelEvent *event)

上面的函数都是虚函数,可以重写对应的事件,对其进行捕获。

QEvent可以和QWheelEvent进行转换,下面例子中会有转换举例。

调用 QEvent::type()可以返回事件的类型,可以根据类型处理特定的特定的操作,截取部分鼠标的事件类型如下:

2、Qt对象的事件过滤器

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

      如果此对象已安装为监视对象的事件过滤器,则过滤事件。 在您重新实现此函数时,如果您想过滤掉事件,即停止对其进行进一步处理,请返回 true; 否则返回false。

监听Qt的对象事件,有两步:

    (1)Qt安装事件过滤器:void QObject::installEventFilter(QObject *filterObj)

     (2)重写eventFilter(QObject *watched, QEvent *event)

demo代码如下:

/QtWidgetsApplication2.h/
#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication2.h"
#include <QMouseEvent>
#include <QWheelEvent> 
#include <qdebug.h>
class QtWidgetsApplication2 : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsApplication2(QWidget *parent = Q_NULLPTR);


protected:
	void mousePressEvent(QMouseEvent*event);//按下
	void mouseMoveEvent(QMouseEvent*event);//按下移动
	void mouseReleaseEvent(QMouseEvent*event);//松开
	void mouseDoubleClickEvent(QMouseEvent*event);//双击
	void wheelEvent(QWheelEvent*event);//滚轮
	bool eventFilter(QObject *obj, QEvent *ev);
private:
    Ui::QtWidgetsApplication2Class ui;
};




/QtWidgetsApplication2.cpp/
#include "QtWidgetsApplication2.h"
#include <QMap>
#include <string>
QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
    : QMainWindow(parent)
{
	QMap<int64_t, std::string>map1;
	map1[10] = "string";

    ui.setupUi(this);
	ui.pushButton->installEventFilter(this);
}

void QtWidgetsApplication2::mousePressEvent(QMouseEvent * event)
{
	QPoint nPos = event->globalPos();
}

void QtWidgetsApplication2::mouseMoveEvent(QMouseEvent * event)
{
	int nPointX = event->globalX();
}

void QtWidgetsApplication2::mouseReleaseEvent(QMouseEvent * event)
{
	bool bLeft = false;
	if (event->buttons() == Qt::LeftButton)
	{
		bLeft = true;
	}
}

void QtWidgetsApplication2::mouseDoubleClickEvent(QMouseEvent * event)
{
	int nPointY = event->globalY();
}

void QtWidgetsApplication2::wheelEvent(QWheelEvent * event)
{
	QEvent *qEve = static_cast<QEvent*>(event);

	QMouseEvent *qMouse = static_cast<QMouseEvent*>(qEve);
}

bool QtWidgetsApplication2::eventFilter(QObject * obj, QEvent * ev)
{
	QMouseEvent *qMouse = static_cast<QMouseEvent*>(ev);
	if (ev->type() == QEvent::Wheel)
	{
		QRect rt(0,0,50,50);
		int nMouseX = qMouse->pos().y();
		int nMouseY = qMouse->pos().x();
		if (nMouseX < rt.width() && nMouseY < rt.height()) //在该区域不响应鼠标滑轮。
		{
			return true;
		}
	}

	if (obj == ui.pushButton) {
		if (ev->type() == QEvent::KeyPress) {
			QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
			qDebug() << "Ate key press" << keyEvent->key();
			return true;
		}
		else {
			return false;
		}
	}
	else {
		// pass the event on to the parent class
		return QMainWindow::eventFilter(obj, ev);
	}
}


//main.cpp///
#include "QtWidgetsApplication2.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtWidgetsApplication2 w;
    w.show();
    return a.exec();
}

demo的.ui文件如下:

 

 demo的Qt工程配置

 Qt中绝对位置和相对位置介绍的博文:(2条消息) Qt获取鼠标位置(绝对位置、相对位置)_云鹤起舞的博客-CSDN博客_qt获取鼠标位置

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值