June 15, 2011

spode spode
Ant Farmer
317 posts

qml, how to parse text to number

 

i have a Text { text: “15”; id: label }. how do i parse the text into number to make some verification (if(label’s text is minor than 15) add 1 to label’s text and write it into label’s text)

4 replies

June 15, 2011

billouparis billouparis
Ant Farmer
142 posts

Maybe you can write a simple javascript function in your current scope.

  1. function convertToNumber(string)
  2. {
  3.        return parseInt(string)
  4. }

And then call the function in your if condition expression?
Bill

June 15, 2011

spode spode
Ant Farmer
317 posts

you are a qt’s dragon! =)
so, i need to learn javascript. I miss it too!

just a question: where must i write this function…and how can i call it?

June 16, 2011

billouparis billouparis
Ant Farmer
142 posts

You may be able to write it inside your current scope ie.
The following code is working on my PC, it is incrementing the string value “15” to string value “16”:

  1. Rectangle {
  2.     width: 360
  3.     height: 360
  4.     Text {
  5.         anchors.centerIn: parent
  6.         text: incrementString("15");
  7.     }
  8.  
  9.     function incrementString(string)
  10.     {
  11.            var stringValue = String(parseInt(string) + 1);
  12.            return stringValue;
  13.     }
  14.  
  15.  
  16.     MouseArea {
  17.         anchors.fill: parent
  18.         onClicked: {
  19.             Qt.quit();
  20.         }
  21.     }
  22. }

June 16, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

link @ http://doc.qt.nokia.com/4.7/qml-extending-types.html should help you on how to add functions and signal handlers to your qml code.

 
  ‹‹ how to create two non-classical windows in qml?      Strange offset after rotating a rectangle ››

You must log in to post a reply. Not a member yet? Register here!