Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

10K Developer Commitment

Reply
Contributor
mariomunera89
Posts: 15
Registered: ‎10-10-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: N/A
Accepted Solution

dynamically change label text QML

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  
   
}
 
}

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: dynamically change label text QML

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.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Contributor
mariomunera89
Posts: 15
Registered: ‎10-10-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: N/A

Re: dynamically change label text QML

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

Please use plain text.