Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Signal/Slot system inside QML



  • Hello,
    I would like to use a signal/slot system on the QML side.

    I don't know how to achieve this.
    Here is an example of architecure that I want it to work with :
    0_1535622639780_51a9bd39-9cfb-48f8-aab5-eab764e56a7f-image.png

    I want the sub1 and sub 2 to emit a signal.
    This signal should trigger an action on the component2

    I found this on this subject : http://doc.qt.io/archives/qt-4.8/qmlevents.html
    But I don't see how to connect the signal from sub2 to component2.
    What is the best way to do that ?



  • Do you really want to load separate QML documents and connect the objects in those? This is not the intention behind QML. Usually, you have one main file that instantiates the whole object tree like this:

    // Main.qml
    Main {
        // defined in Component1.qml
        Component1 {
            Sub1 {
                // ...
            }
        }
    
        Component2 {
            // ....
        }
    }
    

    Usually, you would just attach signal handlers to signals as shown here instead of creating explicit connections. Slots behave like Javascript functions in QML, so you would write a signal handler like this:

    SomeComponent {
        onSomeSignal { destination.someSlot(); }
    }
    

    But you can also create connections explicitly using the built-in Connections type.

    Does that answer your question?



  • @rweickelt
    I'm not sure to really understand.

    My code at the moment is made like you describe in your message :

    // Main.qml
    Main {
        // defined in Component1.qml
        Component1 {
            Sub1 {
                // ...
            }
        }
    
        Component2 {
            // ....
        }
    }
    

    What I'm doing is that I make some kind of simulator for a display and a controller.
    Let's say my controller is Component 1 and the display is Component 2.
    In my controller is made of 2 kind of button (Sub1 and sub2) those buttons have to emit signal on press but the action has to be done in the display side.
    Here is what I'm exactly trying to do. Maybe I'm not thinking it right, if so please can you guide me to the good solution ?



  • Please read the documentation that I have linked in my reply. It contains a lot of code examples how to work with signals and slots in QML.



  • @rweickelt
    I did (even before you reply, I put this link in my first post) but it shows example only inside the same component. My issue here is that I have multiple component that need to communicate.

    The only way I see for the moment is to create multiple alias in every single qml file. But I'm pretty sure there is another way to do this.


Log in to reply