qtqmlsignalsdisconnectslot

Is it possible to disconnect all slots from a signal in Qt5 QML?


In QML it is impossible to call .disconnect() without arguments for a signal:

file:mainwindow.qml:107: Error: Function.prototype.disconnect: no arguments given

So how can I disconnect ALL slots without specifying each of them? Or maybe it is possible by passing signal object to C++ and disconnect it somehow there? Or maybe any workaround exists?

The goal I want to reach is to change behavior of an object by connecting different slots to it's signal. For example:

object.disconnect() // disconnect all slots
object.connect(one_super_slot)
object.disconnect() // disconnect all slots
object.connect(another_super_slot)

Solution

  • Okay, 5 minutes after my question I've made a workaround: connect only once to one signal that calls jsobject from inside:

    Item {
        property var fire
    
        // Any qml object. In this example it is ActionExecutor which emits actionRequest
        ActionExecutor {
            //signal actionRequest(int actionType)
            onActionRequest: fire(actionType)
        }
    
        Action {
            shortcut: "Ctrl+S"
            text: "One action"
            onTriggered: {
                parent.fire = function(actionType) {
                    console.log('one slot')
                }
            }
        }
    
        Action {
            shortcut: "Ctrl+X"
            text: "Another action"
            onTriggered: {
                parent.fire = function(actionType) {
                    console.log('Another slot')
                }
            }
        }
    }
    

    So that js object can be reassigned many times as you want so you may change your behavior by reassigning this object. If you want to disconnect all simple assign undefined to fire. Also you can make a chain of "slots" by modifying code to something like:

    Item {
        property var fire
        property var slots: [
            function(actionType) {
                console.log('1: ' + actionType)
            },
    
            function() {
                console.log('2: ' + actionType)
            },
    
            function() {
                console.log('3: ' + actionType)
            }
        ]
    
        // Any qml object. In this example it is ActionExecutor which emits actionRequest
        ActionExecutor {
            //signal actionRequest(int actionType)
            onActionRequest: fire(actionType)
        }
    
        Action {
            shortcut: "Ctrl+S"
            text: "One action"
            onTriggered: {
                parent.fire = function(actionType) {
                    console.log('calling all custom JS-slots')
    
                    for (var i in slots) {
                        slots[i](actionType)
                    }
                }
            }
        }
    }
    

    So anyone can implement own signal-slot architecture in qml as a simple javascript observer pattern. Enjoy.