Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot convert QString to int in assignment



  • @
    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?



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



  • 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?



  • 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/



  • Thanks for the nfo



  • [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.



  • 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.


Log in to reply