Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. String to Int in qml
Forum Updated to NodeBB v4.3 + New Features

String to Int in qml

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 5 Posters 67.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zanes
    wrote on 1 Aug 2011, 08:22 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on 1 Aug 2011, 09:24 last edited by
      #2

      try this workaround:
      @text: textInput1.text*1 + 1@

      and, please, use code tag for code snippets

      --
      Vasiliy

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zanes
        wrote on 1 Aug 2011, 09:26 last edited by
        #3

        wow! that worked !!

        Thank you Vass.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blam
          wrote on 2 Aug 2011, 03:24 last edited by
          #4

          Not you can also use the built in JavaScript parseInt() function - e.g.

          @
          text: parseInt(textInput1.text) + 1
          @

          1 Reply Last reply
          2
          • S Offline
            S Offline
            srikanth_trulyit
            wrote on 3 Aug 2011, 05:55 last edited by
            #5

            In your code string + int evaluates to string as per javascript expressions. Use parseInt for self explainable code.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QtQueries
              wrote on 3 Aug 2011, 07:06 last edited by
              #6

              @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 Reply Last reply
              2

              1/6

              1 Aug 2011, 08:22

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved