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. Try parse QString to int
Forum Updated to NodeBB v4.3 + New Features

Try parse QString to int

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 4.4k 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.
  • C Offline
    C Offline
    column
    wrote on last edited by
    #1

    Hello,

    Looking for function that parses QString to int. In case it can't parse , for example, string contains some non numeric's it should return error.

    Something like tryparse() in other languages.

    J.HilkJ 1 Reply Last reply
    0
    • C column

      Hello,

      Looking for function that parses QString to int. In case it can't parse , for example, string contains some non numeric's it should return error.

      Something like tryparse() in other languages.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @column
      lucky, you're using Qt:
      http://doc.qt.io/qt-5/qstring.html#toInt


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        Hello,

        You can use QString::toInt() method. Which take an optional boolean pointer as argument, giving the result of the conversion.

        Just a small example with output

        #include <QCoreApplication>
        #include <QDebug>
        
        void test1();
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            test1();
            return 0;
        }
        
        
        void test1_toInt(const QString &s){
            bool ok;
            int i = s.toInt(&ok, 10);
            qInfo() <<  s << " toInt(dec) -> " << i << (ok ? "OK" : "FAILED");
            i = s.toInt(&ok, 16);
            qInfo() <<  s << " toInt(hex) -> " << i << (ok ? "OK" : "FAILED");
        }
        
        
        void test1(){
        
            test1_toInt("123");
            test1_toInt("12H");
            test1_toInt("H12");
            test1_toInt("FF");
            test1_toInt("GHI");
        }
        

        Output:

        "123"  toInt(dec) ->  123 OK
        "123"  toInt(hex) ->  291 OK
        "12H"  toInt(dec) ->  0 FAILED
        "12H"  toInt(hex) ->  0 FAILED
        "H12"  toInt(dec) ->  0 FAILED
        "H12"  toInt(hex) ->  0 FAILED
        "FF"  toInt(dec) ->  0 FAILED
        "FF"  toInt(hex) ->  255 OK
        "GHI"  toInt(dec) ->  0 FAILED
        "GHI"  toInt(hex) ->  0 FAILED
        
        1 Reply Last reply
        5

        • Login

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