Qt World Summit: Submit your Presentation

Injecting Events From C++

  • Hi all... I just discovered QtQuick a couple of weeks ago... and it seems awesome! :)

    However, I have a small problem integrating it into my existing Qt application.

    My application is designed in such way that it is controlled without mouse or keyboard directly; instead you can connect external input devices (keyboards, XBox controllers, PS3 controllers, etc.) to a separate input controller; this input controller receives input events, "translates" them into application-specific commands, and then passes them to my application through a TCP client/server connection.

    In my original application, each "event listener" had to implement a specific interface and would then register with the application. This approach doesn't seem to work with QML. I could, certainly, achieve a "working" result by manually passing events to ALL children of my root QML object, for example-- but I don't think that's the way to do it.

    Isn't there a "clean" way to inject certain events into QML?

    I hope I made things clear-- if you didn't understand what I'm aiming at or if you need more information, don't hesitate to ask. Any help is appreciated!

  • Hello and welcome to devnet

    bq. Isn’t there a “clean” way to inject certain events into QML?

    So it would be enough to access a QML javascript function? You can communcate in Cpp and QML with signals and slots, it's quite simple to emit a signal which passes some parameters to qml and also the other way. Would that be what you are searching for? If so, then i can provide you more information and code.

    Also it is possible to directly access properties and methods of your QML components.

  • Thank you!

    Yeah, I'm aware of the possibility of communicating between C++ and QML through signals and slots-- in fact that's what I'm using to forward an (external) event to the QML root at the moment. Here's an example (which I should have posted to begin with):

    Rectangle {
    id: screen;
    onExternalSignal: {
    // a button was pressed on an external input device
    Rectangle {
    // how do these CHILD elements get notified of the external signal?
    Rectangle {
    // how do these CHILD elements get notified of the external signal?

    As I tried to explain with my initial post, I definitely COULD pass the event to the child items through some QML hacking (at least I guess this will work), whereas in Qt I would simply override "bool event(...)" and handle everything there (including event propagation back to the top).

    Is there a similar functionality possible in QtQuick?

  • Do you know about all the children or not? If it should only notify the children you basically know you can access the id of your children and some function from your children which handles the children based on the signal or parameters. Otherwise you can iterate trough all children and subchildren if that is necessary and you dont know or have id's.

  • Alright, I figured it out-- sort of.

    When my Qt application initializes, it tells my custom QQuickView implementation to recursively parse all children-- if the current child is in fact an instance of my custom QQuickItem class, it gets added to a list. I do this upon initialization only, as anything else would be too heavy during runtime.

    Now I just need to make sure I don't create items "on the fly" someday...

    Anyway, whenever my Qt application receives an external event, it now signals the event to the custom quick view, which in turn (using the recursively assembled list) signals it to its relevant child items.

    It's a fairly hackish solution... but whatever... I'm good for now. :)


  • Alright, glad to see you found a solution. I am also parsing all children recursively. If i couldn't find a children in my list when i try to access it trough my class, it'll search the RootObject again for all children and add it to the list if it found it. This would fix the problem with dynamic object creation in qml, well at least for my purposes. For your purposes i would go ahead and add a signal on which it'll update the list of objects emitted by every created qml-component/object.

Log in to reply