Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Send KeyEvent from QWidget to QML element
Forum Updated to NodeBB v4.3 + New Features

Send KeyEvent from QWidget to QML element

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 5 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    apap_
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

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

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        apap_
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            apap_
            wrote on last edited by
            #5

            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)

            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                apap_
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  Have you made sure the keyPressEvent() is called ?

                  157

                  1 Reply Last reply
                  0
                  • GianlucaG Offline
                    GianlucaG Offline
                    Gianluca
                    wrote on last edited by
                    #9

                    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.

                    S 1 Reply Last reply
                    0
                    • GianlucaG Gianluca

                      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.

                      S Offline
                      S Offline
                      sraboisson
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved