Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.2.4
-
None
Description
If a mouse filter contains another mouse filter and the child mouse filter accepts and filters an event, the parent filter will still receive the event.
Example
EventObserver.h
#ifndef EVENTOBSERVER_H #define EVENTOBSERVER_H #include <QQuickItem> class EventObserver : public QQuickItem { Q_OBJECT public: EventObserver(); signals: // QQuickItem interface protected: bool childMouseEventFilter(QQuickItem* item, QEvent* event); }; #endif // EVENTOBSERVER_H
EventObserver.cpp:
#include "eventobserver.h" EventObserver::EventObserver() { this->setFiltersChildMouseEvents(true); } bool EventObserver::childMouseEventFilter(QQuickItem* item, QEvent* event) { qDebug() << this->objectName() << "received event of type:" << event->type(); qDebug() << "event:" << event; event->accept(); return true; }
main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import tech.example import QtQuick.Controls Window { width: 640 height: 480 visible: true title: qsTr("Hello World") EventObserver { objectName: "High Observer" anchors.fill: parent EventObserver { objectName: "Low Observer" anchors.fill: parent TextArea { anchors.fill: parent } } } }
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "eventobserver.h" int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qmlRegisterType<EventObserver>("tech.example", 1, 0, "EventObserver"); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
If you run the example you'll see that the parent filter detects the event, even after the child filter filters the event. Ideally once an event is filtered it shouldn't be passed to any other filters.