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?
Code:
{
....
connect(timer, SIGNAL(timeout()), this, SLOT(label_update()));
timer->start( 50 );
//works fine so far
}
void MainWindow::label_update()
{
...
ui->label_z->setText( v.toString() ); //WORKS
v = glWidget->mouse_X;
ui->label_mx_pos->setText( v.toString() ); //WORKS
v = glWidget->mouse_Y;
ui->label_my_pos->setText( v.toString() ); //WORKS
v = glWidget->keyval;
ui->label_keyval->setText( v.toString() ); //DOESNT
}
--> OpenGL widget
GLWidget::GLWidget()
{
startTimer( 11 );
rotX = rotY = rotZ = 0.f;
col = 0;
keyval= 0;
....etc
}
void GLWidget::initializeGL()
{
...start stuff...works fine
}
void GLWidget::paintGL()
{
draw stuff...works fine
}
void GLWidget::resizeGL(int width, int height)
{
..
}
{
switch( e->key() )
{
case Qt::Key_Escape:
keyval=111; // dummy val - just confirm it works then hone
}
// I have even commented out above code and set keyval = to some number for any key event.
}
{
//works great
lastPos = event->pos();
}
{
//works great
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
mouse_X = event->x();
mouse_Y = event->y();
lastPos = event->pos();
}
Re: keyPressevent not working with timer.
Quote:
Originally Posted by
T1001
Update label for ANY keypress should read 111...right?
No, only for ESC.
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.
Code:
{
/*
switch( e->key() )
{
case Qt::Key_Escape:
keyval=111;
}
*/
keyval = 111; // the label should read 111 if any keypress is occuring
}
{
lastPos = event->pos();
// if I put keyval = 111 here, it shows in the label with mousepress
}
Re: keyPressevent not working with timer.
Do you have keyboard focus on your GLWidget?
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?
Code:
MainWindowClass....
glWidget = new GLWidget;
ui->openglLayout->addWidget( glWidget );
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?
Code:
GLWidget::GLWidget
{
...
...
setFocusPolicy( Qt::StrongFocus );
}
{
setFocus();
updateGL();
}
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.