Adapt OpenGL Code to Qt (especially keyPressEvent)
-
I have a working program in OpenGL which shall now work under Qt. Some things are a bit different and I am struggling right now with the keypressevent-function. I have a global variable which should be changed by pressing a key (later by presing a button).
@
int modus = -1;void GLWidget::keyPressEvent(QKeyEvent* event) { switch (event->key()) { case Qt::Key_F2 : modus = 1; updateGL(); break; default: break; } }
@
This changed variable should be used in mousePressEvent()
@
void GLWidget::mousePressEvent(QMouseEvent* e)
{
if (modus == 1 && (e->buttons() & Qt::LeftButton))
{
counter++;
p_list[counter].x = mx - width1/2;
p_list[counter].y = my - height1/2;
glPointSize(5);
glColor3d(1, 0, 0);
updateGL();
}
}
@
Thanks for your help -
Could you specify what actually seems to be the problem? The variable is not being set in QKeyEvent or what? Also, where exactly is 'modus' declared? Do you want those events to fire in parallel, or sequentially (from the text, I guess it's sequential, in which case your approach seems to be OK)?
-
The variable is not being set in QKeyEvent! Yes, thats what is my problem. I want to use the variable in the mousePressEvent, but the value isnt changing.I have defined the variable modus globally.
Thanks
-
Strange. I would blame the variable definition, but you say it worked in pure OpenGL app. Have you tried debugging to see what really happens?
-
Thanks for your reply.
It seems that i dont get into the function keypressevent. Even print-commands are not displayed. There is no reaction by clicking any button. I mean, even if i try different buttons in the code.It is strange. Any ideas? -
That's very, odd. Does the mouse event function work/get called (i.e is it only the keyPressEvent) ?
-
OK I've searched the docs a bit, and here is one possible cause:
A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.I'll search some more now.
-
That might indeed be the problem.
You can easily check which widget is the keyboard grabber by calling "QWidget::keyboardGrabber();":http://doc.qt.nokia.com/4.7/qwidget.html#keyboardGrabber -
Thanks for your replies. The problem was that i used the keypressfunction in the wrong class. My mistake. Nevertheless I thank you for your anwers.