Search code examples
qtqmlqtquick2observer-patternsignals-slots

Connect signal to slot, but call the slot only once and then auto-disconnect them


Consider this JS code:

function handleSig() {
    emitter.someSig.disconnect(handleSig);
    // do some work here
}

emitter.someSig.connect(handleSig);

Can it be written without the explicit disconnect and the named function?

Ideally, I'd like something like this:

emitter.someSig.connect(
    function() {
        // do some work here
    },
    Qt.SingleShotConnection
);

Near-duplicate: Automatically disconnect after first signal emission - but that question is about Python and mine is about QML and C++.


Solution

  • You can create a small helper function, which does the disconnecting for you, like such:

    function connectOnce(sig, slot) {
        var f = function() {
            slot.apply(this, arguments)
            sig.disconnect(f)
        }
        sig.connect(f)
    }
    

    As demonstration of the usage:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    ApplicationWindow {
        id: myWindow
        visible: true
        width: 600
        height: 600
        color: 'white'
    
        signal action(string name)
    
        function slot(name) {
            console.log(name)
        }
    
        Button {
            text: 'connect'
            onClicked: {
                connectOnce(action, slot)
            }
        }
    
        Button {
            y: 80
            text: 'action'
            onClicked: {
                action('test')
            }
        }
    
        function connectOnce(sig, slot) {
            var f = function() {
                slot.apply(this, arguments)
                sig.disconnect(f)
            }
            sig.connect(f)
        }
    }
    

    The upper two Buttons will connect slot and slot2 in single-shot mode to the signal action. The Button action will fire the signal action which will execute the slots as many times as they are connected. Then they will be immediately disconnected.

    You might put the function connectOnce into a library to have it to your avail where ever you need it.


    This solution is easily extended to a more general form, that will connect a function to be executed n times by introducing a counter in the closure:

    function connectN(sig, slot, n) {
        if (n <= 0) return
        var f = function() {
            slot.apply(this, arguments)
            n--
            if (n <= 0) sig.disconnect(f)
        }
        sig.connect(f)
    }