[SOLVED] Key events received by WebView item but not TextInput item (Was: Stuck sending KeyPress events from C++ to Q
-
wrote on 8 Apr 2012, 18:21 last edited by
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!
-
wrote on 9 Apr 2012, 14:24 last edited by
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.
-
wrote on 9 Apr 2012, 14:39 last edited by
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.
-
wrote on 9 Apr 2012, 16:51 last edited by
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!
-
wrote on 20 May 2012, 12:18 last edited by
Do you have a code sample of your sendevent ?
-
wrote on 11 Jun 2012, 20:32 last edited by
@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.