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. Cannot convert QString to int in assignment
Forum Updated to NodeBB v4.3 + New Features

Cannot convert QString to int in assignment

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 11.8k 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.
  • A Offline
    A Offline
    aetbanan
    wrote on 23 Apr 2012, 17:56 last edited by
    #1

    @
    int iTime;
    QString newline;
    bool ok;
    iTime = newline.toInt(&ok, 10);
    @

    This line doesn't build because it "cannot convert QString to int in assignment".

    Why?

    Also, which would be the best place for general questions on Qt-programming like this?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aetbanan
      wrote on 23 Apr 2012, 18:00 last edited by
      #2

      Hoho, it was a very stupid misstake, a truncate operation on newline that didn't happen :)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aetbanan
        wrote on 23 Apr 2012, 18:02 last edited by
        #3

        https://duckduckgo.com/?q=error+"cannot+convert+QString+to+int+in+assignment+qt"

        There are no hits on the search engines for this error though, where can I find documentation on errors?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          m.derempoukas
          wrote on 23 Apr 2012, 19:01 last edited by
          #4

          This is more a c++ related error than qt specific. It has to do with type casting. You tried to assign QString to an int at line 4.
          [quote author="aetbanan" date="1335203805"]@
          int iTime;
          QString newline;
          bool ok;
          iTime = newline.toInt(&ok, 10);@
          [/quote]

          In such situations c++ tries to convert one type to another but those types must be compatible or in case of object assignment should offer copy constructors and/or assignment operators that handle that conversion. The reason you didn't find any related hits on the search engines is that the combination of incompatible types or objects is unlimited and you created one of them.

          For more information on c++ type casting check http://www.cplusplus.com/doc/tutorial/typecasting/

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aetbanan
            wrote on 23 Apr 2012, 22:34 last edited by
            #5

            Thanks for the nfo

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on 24 Apr 2012, 08:25 last edited by
              #6

              [quote author="aetbanan" date="1335203805"]
              @
              int iTime;
              QString newline;
              bool ok;
              iTime = newline.toInt(&ok, 10);
              @
              [/quote]

              This works perfectly. Did you modify your original post? If so, please leave a note - this whole thread doesn't make sense anymore for anyone jumping in later.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • M Offline
                M Offline
                m.derempoukas
                wrote on 24 Apr 2012, 08:43 last edited by
                #7

                Volker is correct. This code works fine but I did the mistake of taking as fact that the error did come up with the above code.This could be created in situation such as

                @int iTime;

                QString newline;

                bool ok;

                iTime = newline(10)@

                where by mistake you missed to use the member function and used QString constructor. I must read more carefully and imagine less I guess.

                1 Reply Last reply
                0

                6/7

                24 Apr 2012, 08:25

                • Login

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