[Solved] Convert std::string to QString in Qt 4.8.1?
-
wrote on 4 Jan 2014, 14:42 last edited by
Hi
I have a std::string myString which needs to be converted to QString?
How do I do this?
I am aware of following method:@QString myQString = QString::fromAscii(myString.c_str(), myString.length());@
But the problem is that I have millions of such strings.
So I would like to know the most efficient method for this translation? -
wrote on 4 Jan 2014, 14:51 last edited by
Wont QString::fromStdString() work ?
-
wrote on 4 Jan 2014, 14:56 last edited by
The documentation says
bq. This constructor is only available if Qt is configured with STL compatibility enabled.
What does this mean?
-
wrote on 4 Jan 2014, 15:51 last edited by
When compiling Qt from source you can disable support for these functions but to my knowledge the pre-compiled distribution is compiled with the support enabled.
However: fromStdString always assumes ascii encoding, so using it is a bad idea unless the strings are internal strings or you're sure your code will never come into contact with non-english languages.
Otherwise your strings might contain utf8 or a local 8 bit encoding (i.e. latin1) so fromAscii or fromStdString will lose information.
You should work with QString::fromLocal8Bit or QString::fromUtf8, depending on where the c-strings come from.
-
wrote on 4 Jan 2014, 16:24 last edited by
[quote author="Tannin" date="1388850668"]When compiling Qt from source you can disable support for these functions but to my knowledge the pre-compiled distribution is compiled with the support enabled.
However: fromStdString always assumes ascii encoding, so using it is a bad idea unless the strings are internal strings or you're sure your code will never come into contact with non-english languages.
Otherwise your strings might contain utf8 or a local 8 bit encoding (i.e. latin1) so fromAscii or fromStdString will lose information.
You should work with QString::fromLocal8Bit or QString::fromUtf8, depending on where the c-strings come from.[/quote]
I believe this has changed in Qt 5.
https://qt-project.org/doc/qt-5.0/qtcore/qstring.html#fromStdString
-
wrote on 5 Jan 2014, 13:38 last edited by
@tanin thanks for the information.
I am working with numbers, so it will always be ASCII.
@t3685 great improvement I think.
4/6