Results 1 to 12 of 12

Thread: QEvent::KeyPress problem, when will this event appear?

  1. #1
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Post QEvent::KeyPress problem, when will this event appear?

    I have a widget and installed event filter to it,

    Qt Code:
    1. bool MyClass::eventFilter( QObject*, QEvent *event )
    2. {
    3. qDebug("EventFilter type: %d", event->type());
    4. QKeyEvent *ke = (QKeyEvent *) event;
    5. int keyValue = ke->key();
    6. if(event->type() == QEvent::KeyPress )
    7. {
    8. switch ( keyValue )
    9. {
    10. case Qt::Key_0: case Qt::Key_1: case Qt::Key_2: case Qt::Key_3 : case Qt::Key_4: case Qt::Key_5: case Qt::Key_6: case Qt::Key_7: case Qt::Key_8: case Qt::Key_9 :
    11. ListBoxA->numClicked((char *)ke->text().latin1());
    12. break;
    13. }
    14. return true;
    15. }
    16. return false;
    17. }
    To copy to clipboard, switch view to plain text mode 


    From the qDebug(), I found that the QEvent::KeyPress appear only once, that is, the first time I press a keyboard input. And all keyboard input afterward seems disappeared, no more can be received. However if I change it to QEvent::KeyRelease, I found that it works!

    Can anyone tell me why is this happening? When will the KeyPress event occur? is it the eventFilter is not working or something else blocked the event?

    QT4.6.3 used.

    Thanks!
    Last edited by batileon; 16th June 2011 at 08:01.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Not sure about your problem but, you should cast the event only after confirming that it is KeyEvent, like this

    moreover it is safer to use c++ style casting

    call the objects event processing if not consuming the event

    Qt Code:
    1. if (event->type() == QEvent::KeyPress) {
    2. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    3. qDebug("Ate key press %d", keyEvent->key());
    4. return true;
    5. } else {
    6. // standard event processing
    7. return QObject::eventFilter(obj, event);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Oops, my typo, the casting IS in the if loop actually.
    Qt Code:
    1. bool MyClass::eventFilter( QObject*, QEvent *event )
    2. {
    3. qDebug("EventFilter type: %d", event->type());
    4. if(event->type() == QEvent::KeyPress )
    5. {
    6. QKeyEvent *ke = (QKeyEvent *) event;
    7. int keyValue = ke->key();
    8.  
    9. switch ( keyValue )
    10. {
    11. case Qt::Key_0: case Qt::Key_1: case Qt::Key_2: case Qt::Key_3 : case Qt::Key_4: case Qt::Key_5: case Qt::Key_6: case Qt::Key_7: case Qt::Key_8: case Qt::Key_9 :
    12. ListBoxA->numClicked((char *)ke->text().latin1());
    13. break;
    14. }
    15. return true;
    16. }
    17. return false;
    18. }
    To copy to clipboard, switch view to plain text mode 

    I want to know when QEvent::KeyPress will appear?
    From my program, the following events had been captured:
    QEvent::KeyPress (type no. = 6) [***can be captured only for the first time***]
    QEvent::KeyRelease (type no. = 7)
    QEvent::ShortcutOverride (type no. = 51)
    QEvent::UpdateRequest (type no. = 77)

    Why is there no more keyPress event after my first keyboard press?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Did you try adding "return QObject::eventFilter(obj, event);"?

    Did you install in on ListBoxA?

  5. #5
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QEvent::KeyPress problem, when will this event appear?

    I don't understand, do u mean calling it recursively?

    I did install it to the widget I need.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QEvent::KeyPress problem, when will this event appear?

    It's not recursive, it's just base-class implementation, you should call it like Santosh suggested.

  7. #7
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QEvent::KeyPress problem, when will this event appear?

    I did try, but still there's no more KeyPress event after the first hit.

  8. #8
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Does your code look like this?:

    Qt Code:
    1. bool MyClass::eventFilter( QObject* obj, QEvent *event )
    2. {
    3. qDebug("EventFilter type: %d", event->type());
    4. if(event->type() == QEvent::KeyPress )
    5. {
    6. QKeyEvent *ke = static_cast<QKeyEvent *>(event);
    7. int keyValue = ke->key();
    8.  
    9. switch ( keyValue )
    10. {
    11. case Qt::Key_0: case Qt::Key_1: case Qt::Key_2: case Qt::Key_3 : case Qt::Key_4: case Qt::Key_5: case Qt::Key_6: case Qt::Key_7: case Qt::Key_8: case Qt::Key_9 :
    12. ListBoxA->numClicked((char *)ke->text().latin1());
    13. break;
    14. }
    15. return true;
    16. }
    17. return QObject::eventFilter(obj,event);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Please notice that in following line, QObject can be something else, for example QWidget if that is the base class of MyClass:

    Qt Code:
    1. return QObject::eventFilter(obj,event);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QEvent::KeyPress problem, when will this event appear?

    I don't understand, do u mean calling it recursively?
    It will not cause recursion, should you have figured it out by now....

    I did install it to the widget I need.
    Please understand my question, did you install the filter on ListBoxA? Yes / No?
    You are modifying ListBoxA in the filter code, are you sure this is not causing the problem?

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QEvent::KeyPress problem, when will this event appear?

    I don't believe that key press events are not generated, maybe your widget loses focus and the events are sent to other widgets ? Install this test event filter on QApplication object and check for KeyPress events:
    Qt Code:
    1. bool MyClass::eventFilter( QObject* obj, QEvent *event )
    2. {
    3. if(event->type() == QEvent::KeyPress )
    4. {
    5. qDebug() << "got keyPress on object" << obj;
    6. }
    7. return QObject::eventFilter(obj,event);
    8. }
    9.  
    10. // and somewhere
    11. MyClass * myObj = ...
    12. ...
    13. qApp->installEventFilter( myObj );
    To copy to clipboard, switch view to plain text mode 

    ----
    edit:
    one more question, maybe reimplementing keyPressEvent for ListBox could be better for your task ? I mean, for me it sounds more sensible to handle specific key press behaviour in class method, rather than in event filter. Or is there another reason why you want to do that this way (event filtering vs. reimplementing keyPressEvent) ?
    Last edited by stampede; 16th June 2011 at 14:31.

  11. #11
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Thanks all for your help. I have found the problem and solution.

    ListBoxA actually contains a Q3ListBox list and some other QButtons
    In ListBoxA::numClicked(char* text), it will insert a new item to the Q3ListBox 'list' :
    Qt Code:
    1. list->insertItem( inputText );
    To copy to clipboard, switch view to plain text mode 

    and seems the newly added item has no event filter attachment
    it works fine after I install the event filter to 'list' instead of ListBoxA.

    However, I am still wondering why KeyRelease event can still be fetched in my previous cases. What's the differences between QEvent::KeyPress and QEvent::KeyRelease ???

  12. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QEvent::KeyPress problem, when will this event appear?

    Quote Originally Posted by batileon
    it works fine after I install the event filter to 'list' instead of ListBoxA.
    This is what I suspected, you were installing filer on ListBoxA, and modifying the ListBoxA inside the event filter code. I tried to ask you couple of times.
    Quote Originally Posted by Santosh Reddy
    Did you install in on ListBoxA?
    Quote Originally Posted by Santosh Reddy
    Please understand my question, did you install the filter on ListBoxA? Yes / No?
    You are modifying ListBoxA in the filter code, are you sure this is not causing the problem?
    It's nice that you have found the problem.

  13. The following user says thank you to Santosh Reddy for this useful post:

    batileon (17th June 2011)

Similar Threads

  1. problem with keypress event and QTableView
    By ranna in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2009, 16:13
  2. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2009, 23:08
  3. [Qt4.5] event(QEvent * event) freeze application
    By czlowiekcien in forum Newbie
    Replies: 2
    Last Post: 25th May 2009, 20:25
  4. Problem with KeyPress event and QTableView
    By ranna in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 20:01
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 14:12

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.