Results 1 to 4 of 4

Thread: eventFilter in Console App

  1. #1
    Join Date
    Mar 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default eventFilter in Console App

    QT Version: 4.4.1
    OS: RHEL 5

    I am attempting to create a console application that uses QLocalServer/QLocalSocket for interprocess communication.

    In the program, I stay in the event loop waiting for connections.

    If the user presses a key, I want to exit the program.

    I have remplemented the eventFilter method and added a installEventFilter(this) call to the constructor of the SNOOPER class:

    Qt Code:
    1. class SNOOPER: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SNOOPER(QString sIP); // Constructor
    7. ~SNOOPER(); // Destructor
    8. ...
    9.  
    10. bool eventFilter( QObject *o, QEvent *e );
    11.  
    12. public slots:
    13. void connectClient();
    14. void readParts();
    15. };
    16.  
    17. bool SNOOPER::eventFilter( QObject *o, QEvent *e )
    18. {
    19. bool bRval = false;
    20.  
    21. if (e->type() == QEvent::KeyPress)
    22. {
    23. qDebug("KeyPress event detected.");
    24. bRval = true;
    25. exit(0);
    26. }
    27. else
    28. {
    29. qDebug("Non-KeyPress event detected.");
    30. }
    31. return bRval;
    32. }
    To copy to clipboard, switch view to plain text mode 
    I received the following output when I run the program, but I do not get any key events:

    Qt Code:
    1. Non-KeyPress event detected.
    2. Non-KeyPress event detected.
    To copy to clipboard, switch view to plain text mode 
    Please let me know what I am missing.

    Thanks!
    Karl

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: eventFilter in Console App

    KeyPressEvents are sent only to widgets therefore console applications won't receive them.

  3. #3
    Join Date
    Mar 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: eventFilter in Console App

    Is there a good way to trap <control>-C so that I can at least shutdown the QLocalServer if they exit that way?

    Currently, it works like this:
    1. Start the program.
    2. Hit Control-C to stop it.
    3. Start it again, and I get a socket busy error.
    4. Start it again and it works.

    If I could somehow skip step 3, I'd be alright.

    Karl

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: eventFilter in Console App

    Use signal() or sigaction() depending on the system you are using.

  5. The following user says thank you to wysota for this useful post:

    KaptainKarl (29th December 2008)

Similar Threads

  1. accessing serial port without CONFIG += console
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 22:47
  2. QProcess-startDetached and Console screen problem
    By ramazangirgin in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 09:05
  3. convert console to windows app
    By Max Yaffe in forum Newbie
    Replies: 1
    Last Post: 13th June 2007, 16:38
  4. Console replacement
    By aegis in forum Qt Programming
    Replies: 14
    Last Post: 3rd April 2007, 01:38
  5. How to run a console program "silently"?
    By fullmetalcoder in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2006, 11:03

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.