PDA

View Full Version : keyPressevent not working with timer.



T1001
8th December 2010, 02:56
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?


MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindowClass)
{
....
QTimer *timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(label_update()));
timer->start( 50 );
//works fine so far
}

void MainWindow::label_update()
{
QVariant v;
...
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)
{
..
}

void GLWidget::keyPressEvent(QKeyEvent *e)
{
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.
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
//works great
lastPos = event->pos();
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
//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();
}

Lesiok
8th December 2010, 06:47
Update label for ANY keypress should read 111...right?No, only for ESC.

T1001
8th December 2010, 15:47
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.



void GLWidget::keyPressEvent(QKeyEvent *e)
{
/*
switch( e->key() )
{
case Qt::Key_Escape:
keyval=111;
}
*/

keyval = 111; // the label should read 111 if any keypress is occuring

}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
// if I put keyval = 111 here, it shows in the label with mousepress
}

high_flyer
8th December 2010, 16:34
Do you have keyboard focus on your GLWidget?

T1001
8th December 2010, 16:55
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?


MainWindowClass....

glWidget = new GLWidget;
ui->openglLayout->addWidget( glWidget );

T1001
9th December 2010, 04:55
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?


GLWidget::GLWidget
{
...
...
setFocusPolicy( Qt::StrongFocus );

}

void GLWidget::timerEvent(QTimerEvent *event)
{
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.