Something is fundamentally wrong with my eventFilter, as it skips every single event, and I want to stop everything . I read a lot of documentation on QEvent , eventFilter() , etc., but obviously I am missing something big. Essentially, I'm trying to create my own modal functionality for my QDialog based QDialog . I want to implement my own, since the built-in setModal(true) includes many functions, for example. playing QApplication::Beep() which I want to exclude. Basically, I want to discard all events going to the QWidget (window) that created my popup . What am I still
// popupdialog.h #ifndef POPUPDIALOG_H #define POPUPDIALOG_H #include <QDialog> #include <QString> namespace Ui {class PopupDialog;} class PopupDialog : public QDialog { Q_OBJECT public: explicit PopupDialog(QWidget *window=0, QString messageText=""); ~PopupDialog(); private: Ui::PopupDialog *ui; QString messageText; QWidget window; // the window that caused/created the popup void mouseReleaseEvent(QMouseEvent*); // popup closes when clicked on bool eventFilter(QObject *, QEvent*); };
...
// popupdialog.cpp #include "popupdialog.h" #include "ui_popupdialog.h" PopupDialog::PopupDialog(QWidget *window, QString messageText) : QDialog(NULL), // parentless ui(new Ui::PopupDialog), messageText(messageText), window(window) { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose, true); // Prevents memory leak setWindowFlags(Qt::Window | Qt::FramelessWindowHint); ui->message_text_display->setText(messageText); window->installEventFilter(this); //this->installEventFilter(window); // tried this also, just to be sure .. } PopupDialog::~PopupDialog() { window->removeEventFilter(this); delete ui; } // popup closes when clicked on void PopupDialog::mouseReleaseEvent(QMouseEvent *e) { close(); }
Here is the problem, the filter does not work. Please note that if I write a std::cout inside if(...) , I see that it fires whenever events are sent to window , it just does not stop them.
bool PopupDialog::eventFilter(QObject *obj, QEvent *e) { if( obj == window ) return true; //should discard the signal (?) else return false; // I tried setting this to 'true' also without success }
When a user interacts with the main program, PopupDialog can be created as follows:
PopupDialog *popup_msg = new PopupDialog(ptr_to_source_window, "some text message"); popup_msg->show(); // I understand that naming the source 'window' might be a little confusing. // I apologise for that. The source can in fact be any 'QWidget'.
Everything else works as expected. The event filter is disabled. I want the filter to delete events sent to the window that created the popup; like a mouse click and keystroke until the pop-up window is closed . I expect to be very confused when someone marks a trivial fix in my code.