Results 1 to 6 of 6

Thread: keyPressevent not working with timer.

  1. #1
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default keyPressevent not working with timer.

    Hi folks, have a quick issue here. My mainwindow calls a glWidget which seems to work just fine. It also uses starttimer function to update openGL and a QTimer with connect to update labels that indicate position infomation from the glWidget. glWidget uses the timer to track mouse events perfectly. However, I can't get the keyPressEvent to capture keyboard presses in the same widget that the mouse cursor events are being tracked in. The implementations are right above each other after the GLPaint function, as one would expect. What silly thing am I overlooking here to get it to work? I've tried updating labels with keypresses or even just sending a fictional value if a key is pressed, yet no dice. I've avoided installing an event filter because the timer scenario has been working well so far up to now. I've followed different examples for it on the web and in the forum. But no luck.

    Update label for ANY keypress should read 111...right?

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindowClass)
    2. {
    3. ....
    4. QTimer *timer = new QTimer;
    5. connect(timer, SIGNAL(timeout()), this, SLOT(label_update()));
    6. timer->start( 50 );
    7. //works fine so far
    8. }
    9.  
    10. void MainWindow::label_update()
    11. {
    12. ...
    13. ui->label_z->setText( v.toString() ); //WORKS
    14. v = glWidget->mouse_X;
    15. ui->label_mx_pos->setText( v.toString() ); //WORKS
    16. v = glWidget->mouse_Y;
    17. ui->label_my_pos->setText( v.toString() ); //WORKS
    18. v = glWidget->keyval;
    19. ui->label_keyval->setText( v.toString() ); //DOESNT
    20. }
    21. --> OpenGL widget
    22. GLWidget::GLWidget()
    23. {
    24. startTimer( 11 );
    25. rotX = rotY = rotZ = 0.f;
    26. col = 0;
    27. keyval= 0;
    28. ....etc
    29. }
    30.  
    31. void GLWidget::initializeGL()
    32. {
    33. ...start stuff...works fine
    34. }
    35. void GLWidget::paintGL()
    36. {
    37. draw stuff...works fine
    38. }
    39. void GLWidget::resizeGL(int width, int height)
    40. {
    41. ..
    42. }
    43.  
    44. void GLWidget::keyPressEvent(QKeyEvent *e)
    45. {
    46. switch( e->key() )
    47. {
    48. case Qt::Key_Escape:
    49. keyval=111; // dummy val - just confirm it works then hone
    50. }
    51. // I have even commented out above code and set keyval = to some number for any key event.
    52. }
    53. void GLWidget::mousePressEvent(QMouseEvent *event)
    54. {
    55. //works great
    56. lastPos = event->pos();
    57. }
    58. void GLWidget::mouseMoveEvent(QMouseEvent *event)
    59. {
    60. //works great
    61. int dx = event->x() - lastPos.x();
    62. int dy = event->y() - lastPos.y();
    63. mouse_X = event->x();
    64. mouse_Y = event->y();
    65. lastPos = event->pos();
    66. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keyPressevent not working with timer.

    Quote Originally Posted by T1001 View Post
    Update label for ANY keypress should read 111...right?
    No, only for ESC.

  3. #3
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: keyPressevent not working with timer.

    Doesn't work with escape being pressed. If I set keyval = 111 in the keyPressEvent call and comment out the conditional operator, it should set keyval to 111 with any keypress. But its not. Likewise, if I set keyval=111 in the mousePressEvent below, it works, and is being called. Why isn't keyPressEvent being called?

    Thanks in advance.

    Qt Code:
    1. void GLWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3. /*
    4. switch( e->key() )
    5. {
    6. case Qt::Key_Escape:
    7. keyval=111;
    8. }
    9. */
    10.  
    11. keyval = 111; // the label should read 111 if any keypress is occuring
    12.  
    13. }
    14.  
    15. void GLWidget::mousePressEvent(QMouseEvent *event)
    16. {
    17. lastPos = event->pos();
    18. // if I put keyval = 111 here, it shows in the label with mousepress
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by T1001; 8th December 2010 at 15:51. Reason: code tages
    All Your Base Are Belong To Us

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: keyPressevent not working with timer.

    Do you have keyboard focus on your GLWidget?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: keyPressevent not working with timer.

    I've added the glWidget to the Mainwindow widget canvas as below, for clarification. I don't think I have set the focus. Would that be the setFocus() function?

    Qt Code:
    1. MainWindowClass....
    2.  
    3. glWidget = new GLWidget;
    4. ui->openglLayout->addWidget( glWidget );
    To copy to clipboard, switch view to plain text mode 
    All Your Base Are Belong To Us

  6. #6
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: keyPressevent not working with timer.

    Following the clues...

    In the constructor I set the policy and the call the focus at timer timeout below, which is repeated. No keyboard detection yet from the widget. How do I get the keyboard focus working?
    Qt Code:
    1. GLWidget::GLWidget
    2. {
    3. ...
    4. ...
    5. setFocusPolicy( Qt::StrongFocus );
    6.  
    7. }
    8.  
    9. void GLWidget::timerEvent(QTimerEvent *event)
    10. {
    11. setFocus();
    12. updateGL();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Added after 23 minutes:


    Nope - got it working using the setFocus et al recommendations. Thanks.

    My app doesn't need focus for the individual widgets. The keys used are pretty global. No two widgets use the same key presses. What is the most "elegant solution" here? An event filter to catalog the keypresses in private members to be accessed by the subwidgets? The need to use the setFocus is a solution to a problem I don't really need.
    Last edited by T1001; 9th December 2010 at 04:55.
    All Your Base Are Belong To Us

Similar Threads

  1. killing Qt timer.
    By jaxrpc in forum Newbie
    Replies: 5
    Last Post: 1st June 2010, 00:21
  2. vertical keypressevent not working on graphicsitem
    By creatio.x in forum Qt Programming
    Replies: 3
    Last Post: 25th December 2009, 11:23
  3. implemet a timer
    By adamatic in forum Qt Programming
    Replies: 12
    Last Post: 17th February 2009, 07:31
  4. Timer for application
    By Zergi in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2007, 22:21
  5. Timer call
    By mahe2310 in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2006, 08:57

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.