[Solved]QDialog::eventFilter() can't Filter



  • Hi all

    I want to get rid of QDialog closing when Escape is pressed. So i used eventFilter() like this:

    class ChangeMaster : public QDialog
    {
    protected:
        bool eventFilter(QObject *object, QEvent *event) override;
    ....
    }
    

    and in the source file

    bool ChangeMaster::eventFilter(QObject *object, QEvent *event)
    {
        qDebug() << "EventFilter entered";
       if(object == this && event->type() == QEvent::KeyPress)
        {
            qDebug() << "Event is key press";
            QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);
            if(keyEvent->key() == Qt::Key_Escape)
                return true;
            else
                return false;
        }
    
        return object->parent()->eventFilter(object, event);
    }
    

    When i run the program and press ESC, it always closes. Also it seems eventFilter never runs because there is no qDebug printing saying "EventFilter entered".

    I couldn't figure out why. Probably because i am not doing everything properly. How can i solve this?

    Thanks in advance


  • Moderators

    Hi @Corpse0327
    Did you installEventFilter for QDialog ?



  • @p3c0 Yeah, i realized i forgot that. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.