Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

How to call function from parent QML (feedback) ?



  • Hi. I have two different QMLs main.qml and Child.qml.
    main.qml contains some objects/functions which I want to call from Child.qml.
    For example:

    main.qml:

    Window {
        ....
        Child {
            id: myChildQml
            width: ...
            height: ...
            ...
            fnStart: function () { myTimer.start(); }
        }
        ...
        Timer {
            id: myTimer
            interval: 30
            repeat: true
            running: false
            triggeredOnStart: true
            onTriggered: {
            ...
            }
        }
    }
    

    Child.qml:

    Rectangle {
            id: root
    
            color: "#a1b2c3"
            opacity: 0.8
            anchors.fill: parent
    
            property variant fnStart;
    
            Button {
                id: button
                x: 19
                y: 21
                width: 118
                height: 57
                text: qsTr("Start")
            }
    
            Connections {
                target: button
                onClicked: {
                    root.fnStart;
                }
            }
    }
    

    I tried to pass timer.start via custom property fnStart, but it doesn't work.

    (sorry if this theme is duplicated, but I didn't find any solution, yet)

    Is it possible to realize something like that?
    Any ideas?

    Thanks!



  • @r3d9u11

    Please refer Signal and Handler Event System.

    • Child.qml
    Rectangle {
            id: root
    
            color: "#a1b2c3"
            opacity: 0.8
            anchors.fill: parent
    
            signal startFunction()
            
           Button {
                id: button
                x: 19
                y: 21
                width: 118
                height: 57
                text: qsTr("Start")
    
             onClicked: {
                  root.startFunction();
             }
            }
    }
    
    • main.qml
    Child {
            id: myChildQml
            width: ...
            height: ...
            ...
            onSrartFunction: {
                                        // Please start the timer here.
            }
        }
    

    All the best.



  • Many thanks for useful link to a conception and code example, it works!



  • @r3d9u11

    Cool :)
    All the best.


Log in to reply