Solved eventFilter anywhere in the program.
-
@mrjj Hi, of course I will answer. All this interests me because I want to implement the code in a separate class. This separate class, I want to include to different projects, not thinking about implementation of the code.
Simply put, all things related to eventFilter are placed in a separate class. And then include it where you need it without completing any more code.
However, this is no longer important. By your example, I will be able to come up with something. Thanks again for the advice.
-
@Oleg21
Ok i understand. a utility class. That's not a bad idea.
I have something the same where i included all events names so it can provide better info
when using a filter. ( for spying ;) -
@mrjj @jsulm Hi, I know this is fairly old post but I am facing kind of similar problem so I thought of reviving it for some help. I am working on Image viewing app with support of adding comments to it. My MainWindow class consists of two Qlabel widgets to show images and and one QTextEdit widget to show and write comments. Now I am using eventFilter to scroll through list of images in my directory folder using keyboard arrow up and down key (working fine) and want to implement a Flagging system in comment box which detect keyboard shortcut keys (set by me) to add automatic flags (comments) in textEditor. I need to install another eventFilter for textEditor which will filter only my keyboard shortcuts as I don't want up and down arrow keys to be caught by my textEditor as they are used for navigation through text box. Imagine I press up arrow key through my comment box to access upper line, image changes on imageLabel.
What I tried so far:
- I tried to create new class and getting textEdit widget from my MainWindow class but I came to know widgets can not be accessed from another class
- So I created a class named filter, created eventFilter function in that class header file, added filter.h file in my MainWindow class and installed that class to my textEdit in MainWindow but it is not working. I want to know is this method correct as I am not sure if I am doing okay
I can share code if you understand my problem, Thanks
p.s. I see this thread is solved so I don't know if it is okay to reply to it like thisUPDATE:
I am able to get my filter class in my MainWindow class by adding this line in my MainWindow.cpp constructor : "filter class = new filter class"
But now I am unable to write anything my text editor but it can catch keyboard keys. -
@sogo said in eventFilter anywhere in the program.:
but I came to know widgets can not be accessed from another class
They can, but you should not do that.
You will need to post your code, else it is just guessing. -
@jsulm So here is my code, I will only post relevant parts as it is a bit big and messy
imageviewer.h (MainWindow)
class Filter; class ImageViewer : public QMainWindow { Q_OBJECT public: ImageViewer(QWidget *parent = nullptr); Filter *filter; protected: void closeEvent(QCloseEvent *event); bool eventFilter(QObject* obj, QEvent *event); // This is mainwindow eventFilter for imageLabels private: QTextEdit *textEdit; ......... rest of code }; #endif // IMAGEVIEWER_H
imageviewer.cpp (MainWindow)
ImageViewer::ImageViewer(QWidget *parent) : QMainWindow(parent) , ui(new Ui::ImageViewer) { ui->setupUi(this); filter = new Filter; textEdit = ui->textEdit; textEdit->installEventFilter(filter); ..... rest of constructor }
filter.h
#ifndef FILTER_H #define FILTER_H #include <QMainWindow> #include<QtGui> namespace Ui { class Filter; } class Filter : public QMainWindow { Q_OBJECT public: explicit Filter(QWidget *parent = nullptr); bool eventFilter( QObject * object, QEvent * eve ); ~Filter(); private: }; #endif // FILTER_H
filter.cpp
#include "filter.h" Filter::Filter(QWidget *parent) : QMainWindow(parent) { } Filter::~Filter() { delete this; } bool Filter::eventFilter( QObject * object, QEvent * eve ){ if (eve->type() == QEvent::KeyRelease) { QKeyEvent* keyEven = static_cast<QKeyEvent*>(eve); //QKeyEvent *e = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); if (keyEven->key() == Qt::Key_F8) { qDebug()<<"here"; return true; } } return false; }
Now textEdit can catch my filter class eventFilter but I am not able to write anything in textEditor as it does not show blinking cursor, but it is able to catch keyboard keys.
-
@sogo said in eventFilter anywhere in the program.:
but I am not able to write anything in textEditor as it does not show blinking cursor
That's because you do not pass the events to base class.
See example from documentation:void MyCheckBox::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { // handle left mouse button here } else { // pass on other buttons to base class QCheckBox::mousePressEvent(event); // THIS } }
-
Hi
You steal all keys :)https://wiki.qt.io/How_to_catch_enter_key
bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event) { if (event->type()==QEvent::KeyPress) { QKeyEvent* key = static_cast<QKeyEvent*>(event); if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) { //Enter or return was pressed } else { return QObject::eventFilter(obj, event); } return true; } else { return QObject::eventFilter(obj, event); } return false; }
Also your filter class is a QMainWindow which not needed
class Filter : public QObject
is enough :) -
@mrjj said in eventFilter anywhere in the program.:
Hi
You steal all keys :)Lol this made me laugh, I see what I am doing wrong. I have been reading documentation all day, searching and somehow I skipped this. Thanks @mrjj @jsulm for the help, it works fine now.
Also I changed filter from QMainWindow to QObject, it's long story, Initially I was trying to create two classes in one file so when I called filter pointer to my MainWindow class constructor, it gave me error of QObject and QMainWindow difference, so I set filter to QMainWindow also. But now I made separate files as former didn't worked as expected.
-
Hi,
Technically, your
ImageViewer
class could directly be the event filter which might also be simpler depending on what you want to achieve.You would need something like:
textEdit->installEventFilter(this);
in your ImageViewer constructor. -
@SGaist Hi SGaist, sorry I wasn't reading the post here anymore as my issue is solved but thank you for your response. I explained in my previous post that I can not use same eventFilter due to up and down arrow keys being filtered for my imageLabel which will obstruct me from using these keys on my text box as up and down arrows are used to navigate through text. I needed a separate eventFilter for text box only.
-
@sogo No you don't.
eventFilter
has two parameters so you can use the object parameter to ensure you apply your filter only on the widgets you want to and not on the others.