Reputation: 2923
I'm pretty new to QML. According to the QML documentation at http://doc.qt.nokia.com/4.7-snapshot/qdeclarativescope.html, the Text object should show 15, but instead it only outputs 5. Can anyone help me out?
Text {
id: counter
x: 300;
y: 300;
property int a: 5;
function randNumber(a){
var a = a+10;
return a;
}
text: a;
}
Upvotes: 1
Views: 1367
Reputation: 2742
Neither pass it as an argument to the function nor create a new variable in the randNumber
function using var
. Simply use a
. It should be availble in the function, since the function in inside the Text
QML Component.
This is not working since you are not calling the function anywhere in your program. I assume that you want to call it on the start-up(technically when the Text component is loaded). So you can call it on the Component.onCompleted
function.
Text {
id: counter
anchors.centerIn: parent
property int a: 5;
function randNumber(){
a = a+10;
return a;
}
text: a;
Component.onCompleted: randNumber();
}
Look into this documentation for more examples.
Upvotes: 0
Reputation: 370132
You're never actually calling your randNumber
function, so it will have no effect. If you want to set text
to the result of randNumber(a)
, you should be doing text: randNumber(a)
. Or without defining a function:
text : {
var a = a+10;
return a;
}
If you remove the var
, the a
property will also be set to 15 (with the above code only text
would be set to 15 and a
would remain 5).
Upvotes: 1
Reputation: 17451
The a
in your function is local to the function because you prefaced it with var
. Instead, make it:
function randNumber(a){
a = a+10;
return a;
}
Upvotes: 2