Send KeyEvent from QWidget to QML element
-
wrote on 5 Sept 2014, 12:36 last edited by
Hello,
I have two windows : one QQuickView and one QGLWidget. The process of key events are made in the root item of the main window QQuickView, so it is made in qml with a javascript function (handler Keys.onPressed).
So now I also want to catch keyevents in QGLWidget, it works well with "keyPressEvent(QKeyEvent* event)", and I want to forward it to my root item in QQuickView.
Here is my code :
@
void MyGLWindow::keyPressEvent(QKeyEvent* event)
{
QKeyEvent* eventCopy = new QKeyEvent(event->type(), event->key(), event->modifiers());
QApplication::postEvent(quickView->rootObject(), eventCopy);
}
@It does not work, does anyone understand why ?
-
Hi,
Did you get any errors ? Can you show the QML code ?
-
wrote on 5 Sept 2014, 13:03 last edited by
No errors.
I am working on Mac 10.8 with QT 5.2.1.QML code:
@
FocusScope {
id: rootwidth: 800 height: 600 focus: true Keys.onPressed: { console.log("Keys pressed QML"); if (event.key === Qt.Key_Escape) { // ... } }
@
My trace is never printed when I press from QLWidget.
-
wrote on 5 Sept 2014, 13:40 last edited by
You might be sending the event to the wrong object. Try sending it to the QQuickView object of your QML application.
-
wrote on 5 Sept 2014, 14:02 last edited by
Same thing with QQuickView object.
I also traced pointers and I am sending to the right object, look :
@
root qml=QQuickFocusScope_QML_95(0x7f7f696ac810)quickView->rootObject()= QQuickFocusScope_QML_95 (this = 0x7f7f696ac810 , name= "" , parent = 0x7f7f69476c00 , geometry = QRectF(0,0 800x600) , z = 0 )
quickView= QQuickView(0x7f7f6947beb0)
@
-
wrote on 5 Sept 2014, 14:11 last edited by
Try sending it to @static_cast<QObject*>(QGuiApplication::focusWindow())@
just to make sure that your sending is working. -
wrote on 5 Sept 2014, 14:30 last edited by
I tried your code t3685 and I loop on my function MyGLWindow::keyPressEvent.
So my sending is working. -
Have you made sure the keyPressEvent() is called ?
-
wrote on 16 Oct 2014, 10:20 last edited by
I was stucked on the same problem and I found a workaround.
If you are still trying to solve the problem let me know and I'll tell you what was my workaround. -
I was stucked on the same problem and I found a workaround.
If you are still trying to solve the problem let me know and I'll tell you what was my workaround.wrote on 3 Apr 2015, 14:15 last edited by@Gianluca
Hi Gianluca, i'm facing the same problem, could you please give us your workaroud please ?
Thank you.