Send KeyEvent from QWidget to QML element



  • 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 ?


  • Moderators

    Hi,

    Did you get any errors ? Can you show the QML code ?



  • No errors.
    I am working on Mac 10.8 with QT 5.2.1.

    QML code:

    @
    FocusScope {
    id: root

    width: 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.



  • You might be sending the event to the wrong object. Try sending it to the QQuickView object of your QML application.



  • 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)

    @



  • Try sending it to @static_cast<QObject*>(QGuiApplication::focusWindow())@
    just to make sure that your sending is working.



  • I tried your code t3685 and I loop on my function MyGLWindow::keyPressEvent.
    So my sending is working.


  • Moderators

    Have you made sure the keyPressEvent() is called ?



  • 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.



  • @Gianluca
    Hi Gianluca, i'm facing the same problem, could you please give us your workaroud please ?
    Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.