Search code examples
qtqmlqt5

QML - How to append an action to a signal handler?


I have created a custom timer providing a pause function and elapsed time property. When triggered, elapsedTime is incremented by the 'interval' property amount. I already tested it and it works fine.

// CustomTimer.qml
import QtQuick 2.0

Timer {
    property double elapsedTimeSec: 0.0

    interval: 100
    repeat: true

    onTriggered: elapsedTimeSec += interval/1000
}

I added it into an existing project as an separate QML file. Now I wish to append action to my onTriggered signal-handler to interact and toggle things in my main application. A little code for example:

Rectangle {
    Slider { 
        id: slider
        value: 0.2
    }
    CustomTimer {
        onTriggered: slider.value += 0.1    
    }
}

How can I do that without deleting already-existing, internal onTriggered handler (since those are necessary to the timer process)?


Solution

  • How can I do that without deleting already-existing actions, since those are necessary to the timer process?

    You shouldn't have to worry. QML allows you to connect multiple handlers to a signal. So in your code, both the onTriggered handler in CustomTimer.qml and the one nested under the Rectangle will be executed.

    The sort of overwrite behaviour you're concerned about only occurs with properties. For example, the CustomTimer below will change the timer's interval from 100 to 500. Unlike slots, the value is propagated.

    Rectangle {
        CustomTimer {
            interval: 500
        }
    }