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. Understanding toInt() function to convert QString to int
Forum Updated to NodeBB v4.3 + New Features

Understanding toInt() function to convert QString to int

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 26.5k Views 2 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.
  • J Offline
    J Offline
    jefazo92
    wrote on 21 Jul 2019, 13:48 last edited by
    #1

    Hi everyone,

    I'm trying to convert a QString into an int. I have read the documentation (https://doc.qt.io/qt-5/qstring.html#toInt) but I don't understand the purpose of the *ok pointer in the toInt() function. What I'm trying to do is something as simple as converting QString Number = "1234" into an integer. That's all.

    Any help would be greatly appreciated. Thank you.

    1 Reply Last reply
    1
    • K Offline
      K Offline
      Kerndog73
      wrote on 21 Jul 2019, 14:02 last edited by
      #2

      If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.

      The ok pointer is used for error reporting.

      QString str = "1234";
      bool ok;
      int num = str.toInt(&ok);
      if (!ok) {
        // conversion failed
      }
      
      1 Reply Last reply
      3
      • J Offline
        J Offline
        jefazo92
        wrote on 21 Jul 2019, 14:04 last edited by
        #3

        Hi @Kerndog73 ,

        So basically I could do something as :

        QString str = "1234";
        bool ok;
        int num = str.toInt(&ok);
        if (!ok) {
          cout << "Conversion failed. Repeat conversion" <<endl;
        }
        

        It's just to check errors but should not affect my conversion from QString to int.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kerndog73
          wrote on 21 Jul 2019, 14:07 last edited by
          #4

          That's right, it won't effect the conversion. It's only used for checking errors. If you're confident that the string is a valid integer then you could do str.toInt();.

          1 Reply Last reply
          3
          • J Offline
            J Offline
            jefazo92
            wrote on 21 Jul 2019, 14:10 last edited by jefazo92
            #5

            Thank you @Kerndog73 , I will include it anyways as a checking tool for my program and to perform conversion again if it failed previously. :)

            1 Reply Last reply
            0

            1/5

            21 Jul 2019, 13:48

            • Login

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