Results 1 to 3 of 3

Thread: if(event->type() == QEvent::KeyRelease) gives error

  1. #1
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default if(event->type() == QEvent::KeyRelease) gives error

    Hi

    This is driving me nuts, so hopefully someone has the answer.

    I am re-implementing QApplication to filter all mouse and keyboard events to a widget which is embedded into a tk application.
    tk does not pass on focus properly and the tk app needs some crucial keystrokes forwarded, so I have to check everything
    including checking which child widget is the receiver and forcing focus in some instances.

    I just finished dealing with mouse clicks and went to add keystrokes

    Qt Code:
    1. bool FilterApplication::notify( QObject *receiver, QEvent *event )
    2. {
    3. if(event->type() == QEvent::MouseButtonPress)
    4. qDebug() << "Button Pressed";
    5. if(event->type() == QEvent::KeyRelease)
    6. qDebug() << "Key Released";
    7.  
    8. .......
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    The test for mouse events is fine, but when I try to do the same for key press / release events I get

    FilterApplication.cpp: In member function ‘virtual bool FilterApplication::notify(QObject*, QEvent*)’:
    FilterApplication.cpp:33:29: error: expected unqualified-id before numeric constant
    FilterApplication.cpp:33:29: error: expected ‘)’ before numeric constant
    make: *** [FilterApplication.o] Error 1


    I can cast the event eg.

    Qt Code:
    1. QKeyEvent *K = (QKeyEvent*)event;
    2. if (K->key() == Qt::Key_Escape)
    3. qDebug() << "Escape Pressed";
    To copy to clipboard, switch view to plain text mode 

    That works fine, except that I get 3 key codes instead of 1 for release and I am casting and testing stuff that isn't key events

    Hopefully something simple, but my head is scrambled trying to find it.

    Qt 4.8 on Linux

    regards

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: if(event->type() == QEvent::KeyRelease) gives error

    Maybe a typo or something similar?

    A quick test:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QKeyEvent>
    4. #include <QLineEdit>
    5.  
    6. class Filter : public QObject
    7. {
    8. public:
    9. bool eventFilter(QObject *watched, QEvent *event)
    10. {
    11. switch (event->type()) {
    12. case QEvent::KeyPress:
    13. qDebug() << "Key Press Event for" << watched << ":" << static_cast<QKeyEvent*>(event)->text();
    14. break;
    15. case QEvent::KeyRelease:
    16. qDebug() << "Key Release Event for" << watched << ":" << static_cast<QKeyEvent*>(event)->text();
    17. break;
    18. default:
    19. break;
    20. }
    21.  
    22. return false; // do not filter
    23. }
    24. };
    25.  
    26. int main(int argc, char **argv)
    27. {
    28. QApplication app(argc, argv);
    29.  
    30. app.installEventFilter(new Filter);
    31.  
    32. QLineEdit lineEdit;
    33. lineEdit.show();
    34.  
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: if(event->type() == QEvent::KeyRelease) gives error

    Hi

    Maybe a typo or something similar?
    You would think so wouldn't you?

    This gives the same error for the KeyEvent tests but not the MouseEvent test

    Qt Code:
    1. switch (event->type())
    2. {
    3. case QEvent::KeyPress:
    4. qDebug() << "Key Press Event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
    5. break;
    6. case QEvent::KeyRelease:
    7. qDebug() << "Key Release Event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
    8. break;
    9. case QEvent::MouseButtonDblClick:
    10. qDebug() << "Mouse button doubleclick event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
    11. break;
    12. default:
    13. break;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Error

    -o FilterApplication.o FilterApplication.cpp
    FilterApplication.cpp: In member function ‘virtual bool FilterApplication::notify(QObject*, QEvent*)’:
    FilterApplication.cpp:52:22: error: expected unqualified-id before numeric constant
    FilterApplication.cpp:52:22: error: expected ‘:’ before numeric constant
    FilterApplication.cpp:52:30: error: expected ‘;’ before ‘:’ token
    FilterApplication.cpp:55:22: error: expected unqualified-id before numeric constant
    FilterApplication.cpp:55:22: error: expected ‘:’ before numeric constant
    FilterApplication.cpp:55:32: error: expected ‘;’ before ‘:’ token
    make: *** [FilterApplication.o] Error 1



    I have now found the cause of the error

    In a global header, the Xlib headers required for X forwarding through XSendEvent are included
    #include <X11/Xlib.h>
    #include <X11/keysym.h>


    I suspect there is a #define clash between keysym.h and the Qt KeyEvent headers
    Have not tracked it down yet, but commenting out the includes allows compilation, so I am off again

    regards

Similar Threads

  1. keyrelease problem in qt
    By kinjalp in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2012, 11:30
  2. Replies: 1
    Last Post: 1st December 2011, 21:47
  3. QEvent::KeyPress problem, when will this event appear?
    By batileon in forum Qt Programming
    Replies: 11
    Last Post: 17th June 2011, 05:23
  4. [Qt4.5] event(QEvent * event) freeze application
    By czlowiekcien in forum Newbie
    Replies: 2
    Last Post: 25th May 2009, 20:25
  5. event(QEvent * event)
    By rajaraob in forum Qt Programming
    Replies: 9
    Last Post: 26th February 2007, 20:25

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.