String to Int in qml
-
wrote on 1 Aug 2011, 08:22 last edited by
Hi All,
Regarding textInputs.
for example:
i got 2 TextInputs.@
TextInput{ id: textInput1; text:"1"}
TextInput{id:textInput2; text: textInput1.text + 1} // i'm expecting an output "2" in textInput2 but now the result is "11"
@So how to convert string to int ?
Thanks in advance. -
wrote on 1 Aug 2011, 09:24 last edited by
try this workaround:
@text: textInput1.text*1 + 1@and, please, use code tag for code snippets
-
wrote on 1 Aug 2011, 09:26 last edited by
wow! that worked !!
Thank you Vass.
-
wrote on 2 Aug 2011, 03:24 last edited by
Not you can also use the built in JavaScript parseInt() function - e.g.
@
text: parseInt(textInput1.text) + 1
@ -
wrote on 3 Aug 2011, 05:55 last edited by
In your code string + int evaluates to string as per javascript expressions. Use parseInt for self explainable code.
-
wrote on 3 Aug 2011, 07:06 last edited by
@blam's suggestion is a good one but CAUTION! If you're using parseInt please always always use the radix parameter (as described "here":http://www.w3schools.com/jsref/jsref_parseInt.asp).
parseInt("11") will always result in 11, however parseInt("011"), because the string starts with 0, it will be interpreted as OCTAL, with a result of 9! Hard to track down bugs ahoy. parseInt("011", 10) will give you the result you expect!
1/6