QML:如何将javascript函数作为另一个函数中的参数传递 [英] QML : how to pass javascript function as argument in another function
问题描述
我有QML代码,例如此代码
Item {id:自我函数update(){var visitFunc = self.applyUpdate;innerTraversal(self,visitFunc);}函数reset(){var visitFunc = self.applyReset;innerTraversal(self,visitFunc);}函数innerTraversal(obj,visitFun){console.log(typeof visitFun);if(obj!==自我&&&&& type of visitFun ===功能")visitFun(obj);if(hasChilderns(obj)){var objChilderns = obj.children;for(var i = 0; i< objChilderns.length; i ++){innerTraversal(objChilderns [i]);}}}函数hasChilderns(obj){if(typeof obj.children!=='未定义')返回true;别的返回false;}函数applyReset(obj){if(typeof obj.reset ==='功能')obj.reset();}函数applyUpdate(obj){if(typeof obj.update ==='功能')obj.update();}}
在普通的javascript中,这很酷,但是当我在QML中使用此代码时,坏的事情是visitFun始终具有未定义的类型,并且不起作用.
您知道如何进行这项工作吗?
在QtQuick 2中,您应该能够使用
将函数绑定到属性 Item {//<-声明id:项目属性变体乐趣}item.fun:Qt.binding(function(){doSomething()})//<-定义item.fun//<-无需大括号的调用
因此您可以传递带有通用函数作为参数的对象.
通常,函数重载a也可以用于创建泛型函数,例如创建Button类型:
--- Button.qml物品 {函数fun(){}//<-声明(空虚拟)MouseArea {anchors.fill:父级onClicked:{乐趣();//<-调用}}}------ main.qml--按钮 {id:按钮函数fun(){//<-通过重载定义做一点事}}---
单击该按钮将激活其onClick处理程序,并实际上执行某些操作;).再次,您将使用通用函数而不是函数本身传递对象.
I have QML code, for example this code
Item {
id:self;
function update(){
var visitFunc = self.applyUpdate;
innerTraversal(self,visitFunc);
}
function reset(){
var visitFunc = self.applyReset;
innerTraversal(self,visitFunc);
}
function innerTraversal(obj, visitFun){
console.log(typeof visitFun);
if(obj!== self && visitFun && typeof visitFun ==="function")
visitFun(obj);
if(hasChilderns(obj)){
var objChilderns = obj.children;
for(var i=0 ; i< objChilderns.length ; i++){
innerTraversal(objChilderns[i]);
}
}
}
function hasChilderns(obj){
if(typeof obj.children !== 'undefined')
return true;
else
return false;
}
function applyReset(obj){
if(typeof obj.reset === 'function')
obj.reset();
}
function applyUpdate(obj){
if(typeof obj.update === 'function')
obj.update();
}
}
in normal javascript this works cool, but when I use this code in QML the bad thing is visitFun always has type of undefined, and it does not work..
any idea how to make this work ?
In QtQuick 2 you should be able to bind functions to properties using
Item { //<-- declaration
id : item
property variant fun
}
item.fun : Qt.binding(function(){doSomething()}) //<--defintion
item.fun // <-- invocation without braces
So you could pass an object with a generic function as parameter.
In general, function overloading a can also be used to create a generic function for example to create a Button type:
---Button.qml
Item {
function fun() {} //<-- declaration (empty dummy)
MouseArea {
anchors.fill: parent
onClicked: {
fun(); //<-- invocation
}
}
}
---
---main.qml---
Button {
id: button
function fun() { //<-- defintion by overloading
doSomething
}
}
---
Clicking the button will activate its onClick handler and actually do something ;). Again, you would then pass the object with the generic function not the function itself.
这篇关于QML:如何将javascript函数作为另一个函数中的参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!