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

In QML, you cannot call .disconnect() without arguments for the signal:

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

So, how can I disable ALL slots without specifying each of them? Or perhaps by passing a C++ signal object and disconnecting it somehow? Or maybe there is some workaround?

The goal I want to achieve is to change the behavior of an object by connecting different slots to it. For instance:

 object.disconnect() // disconnect all slots object.connect(one_super_slot) object.disconnect() // disconnect all slots object.connect(another_super_slot) 
+5
source share
2 answers

Well, 5 minutes after my question, I made a workaround: connect only once to one signal, which causes jsobject from the 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 the js object can be reassigned as many times as you want, you can change your behavior by reassigning this object. If you want to disable all simple assignments undefined to fire . You can also create a chain of " slots " by changing the 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) } } } } } 

Thus, everyone can implement their own signal slot architecture in qml as a simple javascript observer template. Enjoy it.

+1
source

Not. I looked at the source code in qv4objectwrapper.cpp and you can see this code:

 void QObjectWrapper::initializeBindings(ExecutionEngine *engine) { engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("connect"), method_connect); engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("disconnect"), method_disconnect); } 

These are just two methods that are added. If you look at the source code for method_disconnect() , you can see that it always requires one or two parameters, including the name of the slot to disconnect.

Unfortunately, there is no disconnectAll() .

+4
source

Source: https://habr.com/ru/post/1209569/


All Articles