Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install event filter on a custom Qt class?

I want to install an event filter on an object of a custom class in Qt. So I created a project such as QtGuiApplication1 on the Qt Designer and created a simple class as myClass as which has a widget and a QGraphicsView for drawing a colored rectangle.

in header file:

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include "myClass.h"

class QtGuiApplication1 : public QMainWindow
{
    Q_OBJECT
public:
    QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
    Ui::QtGuiApplication1Class ui;
    bool eventFilter(QObject *obj, QEvent *ev);
    myClass* myClass_;
};

in .cpp

#include "QtGuiApplication1.h"


QtGuiApplication1::QtGuiApplication1(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    myClass_ = new myClass(this, QRect(100, 100, 200, 200));
    myClass_->installEventFilter(this);
}

bool QtGuiApplication1::eventFilter(QObject * obj, QEvent * ev)
{

    if (obj == myClass_)
    {
        bool hi = true;
    }

    return false;
}

and the myClass code is here:

header file of myClass:

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>
#include <QGraphicsView>

class myClass : public QObject
{
    Q_OBJECT
public:
    explicit myClass(QObject *parent = 0);
    myClass();
    myClass(QWidget* parent, QRect inRect);
private:
    QWidget * widget;
    QGraphicsView* qGraph_back;
    QGraphicsScene* scene_back;

};

#endif /

cpp file of myClass:

#include "myClass.h"

myClass::myClass(QObject *parent) :
    QObject(parent)
{
}

myClass::myClass()
{
}
myClass::myClass(QWidget* parent, QRect inRect)
{
    widget = new QWidget(parent);
    qGraph_back = new QGraphicsView(widget);
    scene_back = new QGraphicsScene(qGraph_back);

    widget->setGeometry(inRect);
    scene_back->setSceneRect(0,0,inRect.width(),inRect.height());
    qGraph_back->setBackgroundBrush(QColor(0, 0, 255, 80));
    qGraph_back->setScene(scene_back);
    qGraph_back->show();
}

I want to get all the events of myClass_ object such as mouse event, But I can't and the eventfilter doesn't work. how to install eventfilter on the object?

like image 690
M.Hu Avatar asked Nov 24 '25 19:11

M.Hu


1 Answers

The event filter will work only for events in your MyClass instance, only. Not for its children.

So, events, such as a mouse click or move, in your qGraph_back will be not visible in your eventFilter method.

When you add a child in a widget, an QChildEvent event is raised. You can use it to install the event filter on all children (and grandchildren, etc.). But, you have to install the event filter on your MyClass before adding the children.

A quick example:

class Listener: public QObject
{
public:
        Listener(): QObject()
        {}

        bool eventFilter(QObject* object, QEvent* event)
        {
            qDebug() << Q_FUNC_INFO << object << event;
            if (event->type() == QEvent::ChildAdded)
            {
                QChildEvent* ev = dynamic_cast<QChildEvent*>(event);
                ev->child()->installEventFilter(this);
            }
            return false;
        }
};

class Widget: public QWidget
{
    public:
Widget(QObject* parent) : QWidget()
{
    installEventFilter(parent);
    QGraphicsView* view = new QGraphicsView(this);
    auto layout = new QHBoxLayout(this);
    layout->addWidget(view);
    layout->addWidget(new QLabel("Foobar"))
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Listener* listener = new Listener();
    Widget* w = new Widget(listener);
    w->show();
    return app.exec();
}

As you can see, the events in the QLabel are now sent to the listener. But, you can't see the events from the view because they are caught by the viewport widget in the QGraphicsView...

You have to handle the case where the added child has a viewport (inherits from QAbstractItemView, etc.) and it becomes more complicated.

So, if you have to know when the user clicks on your view, it would be easier to use signals/slots and not an event filter.

like image 129
Dimitry Ernot Avatar answered Nov 28 '25 03:11

Dimitry Ernot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!