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?
{
....
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks