Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting one element from a QString to int [SOLVED]
QtWS25 Last Chance

Converting one element from a QString to int [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 43.7k Views
  • 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.
  • D Offline
    D Offline
    DouDou
    wrote on 7 Mar 2012, 19:30 last edited by
    #1

    Let's say for example we have
    @
    QString myString = "A1B";
    @

    If I want to convert the second element of QString the "1" into an int, what is the best way to do that. I tried @myString[1].toInt()@ but that didnt work. Thanks

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Iktwo
      wrote on 7 Mar 2012, 19:34 last edited by
      #2

      toInt() works when the string it's a number, but you have A,B, you can read the documentation about QString in http://qt-project.org/doc/qt-4.8/QString.html

      if you know that the number it's always in the second position you could do something like this:
      @
      QString myString = "A1B";
      int number = myString.mid(1,1).toInt();
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DouDou
        wrote on 7 Mar 2012, 19:42 last edited by
        #3

        [quote author="Iktwo" date="1331148873"]toInt() works when the string it's a number, but you have A,B, you can read the documentation about QString in http://qt-project.org/doc/qt-4.8/QString.html

        if you know that the number it's always in the second position you could do something like this:
        @
        QString myString = "A1B";
        int number = myString.mid(1,1).toInt();
        @
        [/quote]

        Thanks for you reply, what i meant to say (but showed up as a reference) is i tried

        @
        int myInt = myString[1].toInt();
        @

        but that didnt work.

        I dont know the exact position of the numbers in the QString. In fact what I would like to do is loop through all elements and convert the ones that are numbers

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on 7 Mar 2012, 19:46 last edited by
          #4

          How do you intend to store multiple numbers (if the string contains more than one)? Originally you were trying to store the results in a single int variable.

          Also, are you trying to convert single digits? Or whole numbers? (That is, does "A123B" need to parse to 123, or to the series of numbers 1, 2, and 3?

          The ultimate answer is going to probably involve parsing the string (perhaps with a QRegExp) and extracting the individual numbers you want to convert, then iteratively doing the string->integer conversion on the extracted strings.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          1
          • I Offline
            I Offline
            Iktwo
            wrote on 7 Mar 2012, 19:55 last edited by
            #5

            I don't understand what you need, if you have strings like:
            "A1B0H1H3J4"

            and you want:
            1 0 1 3 4

            you could do something like this:
            @QString myString = "A1B0H1H3J4";
            int number = myString.mid(1,1).toInt();

            for (int var = 0; var < myString.length(); ++var) {
                bool ok;
                if (myString.at(var).isDigit()){
                    int digit = myString.at(var).digitValue();
                    //DO SOMETHING HERE WITH THE DIGIT
                }
            }@
            
            1 Reply Last reply
            1
            • D Offline
              D Offline
              DouDou
              wrote on 7 Mar 2012, 19:59 last edited by
              #6

              Tha
              [quote author="Iktwo" date="1331150150"]I don't understand what you need, if you have strings like:
              "A1B0H1H3J4"

              and you want:
              1 0 1 3 4

              you could do something like this:

              @
              QString myString = "A1B0H1H3J4";
              int number = myString.mid(1,1).toInt();

              for (int var = 0; var < myString.length(); ++var) {
                  bool ok;
                  if (myString.at(var).isDigit()){
                      int digit = myString.at(var).digitValue();
                      //DO SOMETHING HERE WITH THE DIGIT
                  }
              }@
              

              [/quote]

              Thanks that is exactly what i needed

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mlong
                wrote on 7 Mar 2012, 20:27 last edited by
                #7

                Be sure and edit your first post to add [Solved] to the thread title! Thanks!

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                1

                4/7

                7 Mar 2012, 19:46

                • Login

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