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