Results 1 to 3 of 3

Thread: Handle KeyRelease Events inside a while loop

  1. #1
    Join Date
    Jun 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Handle KeyRelease Events inside a while loop

    Hi,

    I am working on an application where I need to detect Keypress/Release events from inside a while loop

    Below is my code snippet

    Qt Code:
    1. void TabDialog::keyPressEvent(QKeyEvent *event)
    2. {
    3.  
    4. switch (event->key()) {
    5. case Qt::Key_F1:
    6. {
    7.  
    8. qDebug()<<"Inside Key F1 Press Event";
    9. while(i>0 )
    10. {
    11. qDebug()<<"Inside while loop";
    12. usleep(1000000);
    13.  
    14. if(releaseFlag==1)
    15. {
    16. releaseFlag=0;
    17. break;
    18. }
    19. i--;
    20. }
    21.  
    22. releaseFlag=0;
    23. }
    24.  
    25.  
    26. default:
    27. break;
    28. }
    29. }
    30.  
    31. void TabDialog::keyReleaseEvent(QKeyEvent *event)
    32. {
    33. switch (event->key()) {
    34. case Qt::Key_F1:
    35. {
    36. releaseFlag=1;
    37. qDebug()<<"Inside Key F1 Release Event";
    38.  
    39. }
    40. break;
    41. default:
    42. break;
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    I have keypress event where in which I have a while loop which is going to execute for 10 iterations. Now if I release the F1 key (before completing 10 iterations), "releaseFlag" is not set to 1 and while loop is not getting break immediately. It was only after executing the while loop for 10 iterations I can see the change in "releaseFlag" value.

    Below is the output

    Qt Code:
    1. Inside Key F1 Press Event
    2. Inside while loop
    3. Inside while loop
    4. Inside while loop
    5. Inside while loop
    6. Inside while loop
    7. Inside while loop
    8. Inside while loop
    9. Inside while loop
    10. Inside while loop
    11. Inside while loop
    12. Inside Key F1 Release Event
    To copy to clipboard, switch view to plain text mode 

    I want to identity the release event immediately and break the while loop..

    Can someone guide me how to handle this?

    Regards,
    Pavan

  2. #2
    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: Handle KeyRelease Events inside a while loop

    Can someone guide me how to handle this?
    Drop the while/sleep approach and code something based on QTimer. In your code control doesn't have a chance to return to the event loop and process any other events.

  3. #3
    Join Date
    Jun 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Handle KeyRelease Events inside a while loop

    Hi stampede,

    I have replaced while/sleep with timers and now it was working as expected. Thanks a lot

    Regards,
    Pavan
    Last edited by u2bpavankumar; 2nd July 2015 at 12:48. Reason: [solved]

Similar Threads

  1. KeyPress and KeyRelease events not happening as they should
    By hypnotic401 in forum Qt Programming
    Replies: 3
    Last Post: 26th January 2013, 00:49
  2. KeyPress and KeyRelease Events
    By sattu in forum Qt Programming
    Replies: 8
    Last Post: 30th September 2011, 18:23
  3. Replies: 5
    Last Post: 27th April 2010, 11:04
  4. Handle KeyRelease or KeyPress on any row in QTableView
    By AbuYusuf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 20:04
  5. Unable to handle WindowStateChange events
    By TheRonin in forum Qt Programming
    Replies: 1
    Last Post: 6th March 2008, 11:20

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.