QString to std::string
11,727
Solution 1
How to convert QString to std::string?
One of the things you should remember when converting QString to std::string is the fact that QString is UTF-16 encoded while std::string... May have any encodings.
QString qs;
// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();
// or this if you on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();
Solution 2
std::string str(fileName.toStdString());
if you want str to contain a copy of the qstring
Author by
Moaz ELdeen
Updated on June 29, 2022Comments
-
Moaz ELdeen almost 2 years
I'm trying that sample code and it crashes, in visualc++ 2010
QString fileName = "helloworld"; std::string str = fileName.toStdString();
-
Martin Beckett almost 12 years.toStdString() call toAscii() internally so you don't need to know what qt uses itself
-
paulsm4 almost 12 years@Martin Beckett - thank you, you're correct. ALSO: I forgot to mention the "QtTextCodec" class: doc.qt.nokia.com/4.6/qtextcodec.html