Vic*_*voy 6 qt signals disconnect qml slot
在QML中,不可能在.disconnect()没有参数的情况下调用信号:
file:mainwindow.qml:107: Error: Function.prototype.disconnect: no arguments given
Run Code Online (Sandbox Code Playgroud)
那么如何在不指定每个插槽的情况下断开所有插槽?或者也许可以通过传递信号对象C++并以某种方式断开它?或者可能存在任何解决方法?
我想达到的目标是通过将不同的插槽连接到它的信号来改变对象的行为.例如:
object.disconnect() // disconnect all slots
object.connect(one_super_slot)
object.disconnect() // disconnect all slots
object.connect(another_super_slot)
Run Code Online (Sandbox Code Playgroud)
不.我查看了源代码qv4objectwrapper.cpp,你可以看到这段代码:
void QObjectWrapper::initializeBindings(ExecutionEngine *engine)
{
engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("connect"), method_connect);
engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("disconnect"), method_disconnect);
}
Run Code Online (Sandbox Code Playgroud)
这是仅添加的两种方法.如果查看源代码,method_disconnect()可以看到它总是需要一个或两个参数,包括要断开的插槽的名称.
没有disconnectAll()遗憾的是.
好的,在我提出问题 5 分钟后,我做了一个解决方法:仅连接一次到从内部调用 jsobject 的一个信号:
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')
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,该 js 对象可以根据需要多次重新分配,因此您可以通过重新分配该对象来更改您的行为。如果您想断开所有连接,请简单分配undefined给fire. 您还可以通过将代码修改为以下内容来创建“槽”链:
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)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此任何人都可以在 qml 中实现自己的信号槽架构作为简单的 javascript 观察者模式。享受。
| 归档时间: |
|
| 查看次数: |
4260 次 |
| 最近记录: |