10-19-2012 05:36 PM
This is the silliest thing to do in any language but I'm stuck on how to do it in QML.
How to change the text value of a Label in QML? What am I missing..
I'm not sure why is not working. Even with an alias property the text refuses to change. I will appreciate any help.
Thanks and regards.
My code is the following:
Container{
objectName:"formContainer"
id: formContainer
property alias text1: labelTest.text
onCreationCompleted:{
Qt.labelTest = labelTest;
Qt.text1 = formContainer.text1;
}
Label{
id: labelTest
text:"test"
}
TextField{
id: textFieldPass
onFocusedChanged:{
if(focused){
Qt.myFunction();
}
}
}
function myFunction(){
//Enter successfuly to the function
console.log("Qt.labelTest.text:"+Qt.labelTest.text); //Output: undefined
Qt.labelTest.text ="Y U NO change!"; //Does nothing
Qt.text1 ="Y U NO change!"; //Does nothing
}
}
Solved! Go to Solution.
10-19-2012 08:20 PM
Not sure where you found the idea of putting Qt in front of everything, but try this:
Container {
onCreationCompleted: {
labelTest.text = "set second";
}
Label {
id: labelTest
text: "default, set first"
}
TextField {
onFocusChanged: {
console.log("TextField focused", focused);
if (focused) myFunction();
}
}
function myFunction() {
console.log("label text", labelTest.text);
labelTest.text = "set third";
}
}
Not tested, but if you slap that in a Page it ought to resolve your issues unless I made a typo.
10-22-2012 10:39 AM
Hey peter9477 . Your solution works fine, thank you very much. I just need to add an ID for the parent Container and change the call for myFunction() from just myFunction() to parentContainerID.myFunction(), otherwise you could get a ReferenceError: Can't find variable: myFunction().
Thanks and Regards