Cannot convert QString to int in assignment
-
wrote on 23 Apr 2012, 17:56 last edited by
@
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?
-
wrote on 23 Apr 2012, 18:00 last edited by
Hoho, it was a very stupid misstake, a truncate operation on newline that didn't happen :)
-
wrote on 23 Apr 2012, 18:02 last edited by
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?
-
wrote on 23 Apr 2012, 19:01 last edited by
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/
-
wrote on 23 Apr 2012, 22:34 last edited by
Thanks for the nfo
-
wrote on 24 Apr 2012, 08:25 last edited by
[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.
-
wrote on 24 Apr 2012, 08:43 last edited by
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.
6/7