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. [SOLVED] Key events received by WebView item but not TextInput item (Was: Stuck sending KeyPress events from C++ to Q
QtWS25 Last Chance

[SOLVED] Key events received by WebView item but not TextInput item (Was: Stuck sending KeyPress events from C++ to Q

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.1k Views
  • 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.
  • I Offline
    I Offline
    ioncube
    wrote on last edited by
    #1

    Ok, so this should be easy, but after a few hours of multiple approaches, I'm stuck with managing to send simulated key events to a QML TextInput or TextEdit, and wondered if any readers have a working example of this.

    I've been using the qmlviewer so I tried a C++ plugin first, triggering an event by assigning a property, and in one variant explicitly setting the target receiver via a property too, but then also a C++ app rather than using QML viewer. I've tried using sendEvent and postEvent on QApplication and also sendEvent on a scene instance, sending to the top level view, sending to the root object and various other scenarios but all to no avail. The event is a KeyPress with a letter key code and no modifier. I also tried the text string variant of the event and therefore without a key code. I checked of course that the plugin and app methods are being called from QML and that all works fine. It feels like a fundamental issue as probably most if not all of the variants I tried should have worked.

    The platform is Vista with VC9, QtCreator 2.4.1, Qt 4.8

    Any ideas, obvious gotchas to look out for, reasons why the event might be filtered, recommended event target if not the exact target item, any ways to debug event dispatch, how you solved this, or general wisdom most welcome!

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ioncube
      wrote on last edited by
      #2

      Digging further it turns out that events are being delivered as they can be captured using a Keys.onPressed handler, including one in a TextInput/Edit item, but events are not being picked up by a TextInput or TextEditor item itself as if natively pressed. They are delivered successfully to a focussed field in a WebView item though, which is another control where I need this to work, so it's a partial success. So the mystery is now why custom key events are not delivered to TextInput / TextEdit items but they are to other QML items.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ioncube
        wrote on last edited by
        #3

        Playing further still, it turns out that navigation keys are processed, so a Qt::Key_Left is processed as expected, but not a Qt::Key_A. Frustratingly close but still no cigar.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ioncube
          wrote on last edited by
          #4

          Finally solved. I switched to passing the string version of the key as the 4th parameter when constructing the event, which the documentation suggests is optional, and this then works. All examples of simulating key presses that I found fail to do this, including the code in Trolltech's own test suite for injecting characters and checking that a read-only TextEdit control doesn't change with events; with their test code the TextEdit wouldn't change even if the control weren't in read only mode. Hope this helps someone else some day!

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

            Do you have a code sample of your sendevent ?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BIGGBYpirate
              wrote on last edited by
              #6

              @ioncube, thank you so much. After several hours of trying all the things you tried, I came across your post. Interestingly, when using this method, the TextInput seems to ignore the Qt::Key of the QKeyEvent, and just uses the text. It also ignores modifiers. I wonder why this is; I couldn't find anything about it in any documentation.

              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