Use a function in qml from another qml
-
Hello,
i want to use an function in qml what is written in an other qml file, but it won't work
the example
the function is written in the file "function.qml" in an seperate directory called components
the function looks like this
function assign(id) {
value = id + 20
return: value;
}now i work in another qml file it looks like that.
import "components/"
property int test: function.assign(number)
if i now run the qml I got an message in the qml-log "ReferenceError: Can't find variable: function"
i don't understand it. have someone an idea?
if i wrote the function in the same file it work.
can i call a function like this?NOTE: this is just an example for the function
-
welcome to forum. Did you create the instance of second qml component inside the first qml ? You need create instance, assign the id and through id you need to call.
Try this.
@=== TestFunction.qml====
Rectangle {
width: 100
height: 100
function callme(val, val){
console.log("I am called man="+val + " ok="+val)
}
}
====== main.qml========
Rectangle {
width: 640
height: 480TestFunction{ id : func } Button { text: qsTr("Hello World") anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onClicked: { func.callme("pthinks.com","Bangaluru") } }
}
@ -
welcome to forum. Did you create the instance of second qml component inside the first qml ? You need create instance, assign the id and through id you need to call.
Try this.
@=== TestFunction.qml====
Rectangle {
width: 100
height: 100
function callme(val, val){
console.log("I am called man="+val + " ok="+val)
}
}
====== main.qml========
Rectangle {
width: 640
height: 480TestFunction{ id : func } Button { text: qsTr("Hello World") anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onClicked: { func.callme("pthinks.com","Bangaluru") } }
}
@@dheerendra great dheerendra it worked for me Thanks a lot :)