Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QDialog::eventFilter() can't Filter

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

Scheduled Pinned Locked Moved General and Desktop
qdialogeventfilter
3 Posts 2 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Corpse0327
    wrote on last edited by Corpse0327
    #1

    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

    p3c0P 1 Reply Last reply
    0
    • C Corpse0327

      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

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @Corpse0327
      Did you installEventFilter for QDialog ?

      157

      C 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @Corpse0327
        Did you installEventFilter for QDialog ?

        C Offline
        C Offline
        Corpse0327
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved