Results 1 to 2 of 2

Thread: No keyboard events with QCoreApplication

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default No keyboard events with QCoreApplication

    Hi!

    I'm running a simple command line application based on QCoreApplication and for some reason I am not receiving keyboard inputs. When in run this program in the command line on Linux using ./Application, I can see the keys that I press on the keyboard and but there is no reaction from the keyPressEvent. Can anyone point out where my mistake is?

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. Application w;
    5. a.installEventFilter(&w);
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Application : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Application(QObject *parent = 0);
    7. ~Application();
    8.  
    9. protected:
    10. void keyPressEvent(QKeyEvent *event);
    11. };
    12.  
    13. void Application::keyPressEvent(QKeyEvent *event)
    14. {
    15. qDebug() << "keyPressEvent event occured" << event; // This is never printed.
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No keyboard events with QCoreApplication

    You need to add the protected method QObject::eventFilter() to your Application class instead of the keyPressEvent):

    Qt Code:
    1. class Application : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Application(QObject *parent = 0);
    7. ~Application();
    8.  
    9. protected:
    10. bool eventFilter( QObject * pObj, QEvent * pEvent ) override;
    11. };
    12.  
    13. bool Application::eventFilter( QObject * pObj, QEvent * pEvent )
    14. {
    15. if ( pEvent->type() == QEvent::KeyPress )
    16. {
    17. // got a key press
    18. return true;
    19. }
    20. else
    21. return QObject::eventFilter( pObj, pEvent );
    22. }
    To copy to clipboard, switch view to plain text mode 

    Because your current Application class implementation does not define this method, the base class QObject's method is called instead.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Cruz (16th January 2024)

Similar Threads

  1. Replies: 2
    Last Post: 28th February 2011, 18:15
  2. Filtering all mouse/keyboard events
    By maverick_pol in forum Qt Programming
    Replies: 9
    Last Post: 27th November 2008, 17:03
  3. Replies: 4
    Last Post: 20th February 2007, 12:35
  4. Grab keyboard events in Windows
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 19:56
  5. handling keyboard events from a console app
    By g.cavallin in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2006, 22:31

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.