NelDav
NelDav

Reputation: 910

Emit event of an object before eventFilter trigers

I want to add an event to an object. Therefore I created a class that implements a eventFilter.

Minimal example:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <iostream>

#include <QVBoxLayout>

#include "eventwatcher.cpp"
#include "mynewlineedit.cpp"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QVBoxLayout *mainLay = new QVBoxLayout();

    MyNewLineEdit *lineEdit;

    for(int i = 0; i < 3; i++)
    {
        lineEdit = new MyNewLineEdit();
        mainLay->addWidget(lineEdit);
        new EventWatcher(lineEdit);
    }

    QWidget *centWid = new QWidget();
    centWid->setLayout(mainLay);

    setCentralWidget(centWid);
}



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


eventwatcher.cpp:

#include <QWidget>
#include <QDebug>
#include <QKeyEvent>

#include <iostream>

class EventWatcher : public QObject
{
public:
   explicit EventWatcher(QWidget* parent = Q_NULLPTR) : QObject(parent)
   {
       if (parent)
       {
           parent->installEventFilter(this);
       }
   }

   virtual bool eventFilter(QObject*, QEvent* event) Q_DECL_OVERRIDE
   {
       if (event->type() == QEvent::KeyPress)
       {
           std::cout << "Pressed any key\n";
       }

       return false;
   }
};


mynewlineedit.cpp

#include <QLineEdit>
#include <iostream>

class MyNewLineEdit : public QLineEdit
{
public:
    MyNewLineEdit() : QLineEdit()
    {}

protected:
    void keyPressEvent(QKeyEvent *) Q_DECL_OVERRIDE
    {
        std::cout << "Event of the parent object\n";
    }
};

Add CONFIG += console to the .pro file

The project is a normal Qt-Widgets-Application all other files are not modified.

The result is the following:

Pressed any key

Event of the parent object


The eventFilter it self works fine. But there is a problem with the order of the event handling.

The eventFilter is executed before the KeyPressEvent of the parent object is triggered. But for me, it is important, that the event Filter trigers, after the KeyPressEvent of the parent object.

The result I want to get is the following:

Event of the parent object

Pressed any key

Is there any way to define the order of execution?

Upvotes: 1

Views: 52

Answers (1)

Sumit
Sumit

Reputation: 1525

Here is the solution that I have come-up with:

  • Rename your mynewlineedit.cpp to mynewlineedit.h , change everywhere
  • And change eventwatcher.cpp to this:

#include <QWidget>
#include <QDebug>
#include <QKeyEvent>

#include <iostream>
#include <mynewlineedit.h>
class EventWatcher : public QObject
{
    QWidget* m_parent;
public:
   explicit EventWatcher(QWidget* parent = Q_NULLPTR) : QObject(parent), m_parent(parent)
   {
       if (parent)
       {
           parent->installEventFilter(this);
       }
   }

   virtual bool eventFilter(QObject*, QEvent* event) Q_DECL_OVERRIDE
   {
       if (event->type() == QEvent::KeyPress)
       {
           if(m_parent)
           {
               dynamic_cast<MyNewLineEdit*>(m_parent)->keyPressEvent(dynamic_cast<QKeyEvent*>(event));
           }
           std::cerr << "Pressed any key\n";
           return true;
       }

        return false;
   }
};

Upvotes: 1

Related Questions