在QML中,.disconnect()
没有参数就不能调用信号是不可能的:
file:mainwindow.qml:107: Error: Function.prototype.disconnect: no arguments given
那么,如何在不指定所有插槽的情况下断开所有插槽的连接?还是有可能通过将信号对象传递到C++
并以某种方式断开它的连接?也许存在任何解决方法?
我要达到的目标是通过将不同的插槽连接到其信号来更改其行为。例如:
object.disconnect() // disconnect all slots
object.connect(one_super_slot)
object.disconnect() // disconnect all slots
object.connect(another_super_slot)
好的,在我提出问题后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')
}
}
}
}
这样,您可以根据需要多次重新分配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)
}
}
}
}
}
因此,任何人都可以在qml中实现自己的信号时隙架构,作为简单的javascript观察者模式。请享用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句