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

String to Int in qml

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 5 Posters 68.3k 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 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 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 last edited by
        #3

        wow! that worked !!

        Thank you Vass.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blam
          wrote on 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 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 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

              • Login

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