PDA

View Full Version : QGLWidget, keyPressEvent does not work



ricardo
1st July 2009, 20:46
Hi friends!

I created a class that derives from QGLWidget, and I rewrote keyPressEvent:

void CGLEditorRenderer::keyPressEvent(QKeyEvent* e) {
qDebug("keyPressEvent");
}

But if I press a key I never see that message.

What's going on? Any idea?

Thanks.

nish
2nd July 2009, 03:56
give your glwidget some focus first.. or show us some code

Lykurg
2nd July 2009, 08:07
From a previous post I don't find right now:
install that event handler in your "QMainWindow" and see to which widget the key event is delivered:

bool XXX::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
qWarning() << "The bad guy which steals the keyevent is" << o;
}
return false;
}

ricardo
2nd July 2009, 12:29
Thanks a lot for replies.

@MrDeath: I guess my opengl componet has focus, because I draw some objects without porblems (actually, I want that if user press ESC, drawing is cancelled)

@Lykurg: I wrote these lines in my window class:



bool CEditor::eventFilter(QObject *obj, QEvent *event) {
if(event->type() == QEvent::KeyPress) {
qDebug(obj->objectName().toAscii());
return true;
} else {
return QObject::eventFilter(obj, event);
}
}


And this in the main window constructor installEventFilter(this);

But everytime I press a key, I see CEditorClass, I never see any other string and it doesn't matter if I pressthe key on other object. BTW, it seems SPACE does not work.

Any idea? Honesly, I don't know what going on is.

Thanks in advance.

Lykurg
2nd July 2009, 13:59
have you reimp the key press handler also in your class CEditorClass? If so, you need to pass the event through.

nish
3rd July 2009, 03:30
are you writing
qApp->installEventFilter(this);

simply installEventFilter(this); will not work.

ricardo
3rd July 2009, 11:23
are you writing
qApp->installEventFilter(this);

simply installEventFilter(this); will not work.

Thanks Lykurgand MrDeath, it seems we are closer.


qApp->installEventFilter(this); works better :)
Now I see different objects when keyboard is pressed. But never my opengl component, even if I click it and I press any key (in this case, I see eventfilter says another component).
It's as if my OGL widget never could had focus.

May I missing something?
I don't have reimplemented keyPressed in any place.

By the way, maybe this helps. I'm using Qt Designer, but as it does not have an OpenGL compent I added a horizontal layout, and I added tyhis lines in my window constructor.

// create opengl widget
m_gl_drawer=new CGLEditorRenderer(this);
ui.horizontalLayout_1->addWidget(m_gl_drawer);

Thanks a lot.

ricardo
3rd July 2009, 11:28
OK. It seems now it works.

I read this on docs:

"The policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing, Qt::ClickFocus if the widget accepts focus by clicking, Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all."

And

"You must enable keyboard focus for a widget if it processes keyboard events. This is normally done from the widget's constructor. For instance, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus)."

After adding setFocusPolicy(Qt::StrongFocus) in my OGL component, it works. It seemed to be focus was disabled.

Thanks.