Results 1 to 5 of 5

Thread: QApplication handle all event, why does not include QKeyEvent?

  1. #1
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QApplication handle all event, why does not include QKeyEvent?

    My Qt GUI application has a main dialog, and I want all key event to be processed in the dialog but not its children widgets (event they get focus), so I install a event filter for qApp in main():

    QApplication app(argc, 0);
    MyDialog * pDlg = new MyDialog(0, Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    qApp->installEventFilter(pDlg);

    Then in MyDialog.cpp:

    bool MyDialog::eventFilter(QObject * watched, QEvent * event)
    {
    if (watched == qApp)
    {
    if (event->type() == QEvent::KeyPress)
    {
    // do something
    return true; // break point 1
    }
    return false; // break point 2
    }
    return QDialog::eventFilter(watched, event);
    }

    I set 2 breakpoints. During debugging, the breakpoint 2 can be reached, it means that qApp dispatch events to dialog's eventfilter. But breakpoint 1 never been reached when I press any key. Why the QKeyevent not handled in qApp ?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QApplication handle all event, why does not include QKeyEvent?

    From the friendly docs:
    Key events are sent to the widget with keyboard input focus when keys are pressed or released.
    Your filter will see the key press events but they are not directed at the QApplication instance, i.e. watched != qApp.

  3. #3
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QApplication handle all event, why does not include QKeyEvent?

    Quote Originally Posted by ChrisW67 View Post
    From the friendly docs:

    Your filter will see the key press events but they are not directed at the QApplication instance, i.e. watched != qApp.

    I thought when a widget in main dialog get focus, then press key, the QKeyEvent should be handled by qApp, then forward to the focused widget. That is, qApp handle all events and be responsible for dispatching them, am I wrong ?

    I want process all key press event for all widgets in main dialog, so I don't want to install event filter for every children widgets one by one.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QApplication handle all event, why does not include QKeyEvent?

    I thought when a widget in main dialog get focus, then press key, the QKeyEvent should be handled by qApp, then forward to the focused widget.
    This is almost correct.
    The system key event is handled by QApplication, but that event is not yet a QEeyEvent.
    QApplication will create and dispatch a QKeyEvent if there is a widget that has to handle it (at least that is the way I understand it).
    You might want to use QCoreApplication::winEventFilter().
    And you might want to read about QAbstractEventDispatcher.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QApplication handle all event, why does not include QKeyEvent?

    I made a stupid mistake. if (watched == qApp) is wrong, the "watched" is the obj that is ready to receive the event, but not the qApp whose events are forword to the watcher.

Similar Threads

  1. Can't open include file QApplication
    By Yes in forum Newbie
    Replies: 0
    Last Post: 5th November 2011, 17:55
  2. Replies: 3
    Last Post: 4th October 2010, 16:39
  3. Replies: 2
    Last Post: 26th June 2010, 21:27
  4. Replies: 7
    Last Post: 10th September 2007, 20:35
  5. how can I get the QApplication handle by process ID in QT2
    By pencilren in forum Qt Programming
    Replies: 5
    Last Post: 28th August 2007, 21:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.