How to convert QString to std::string?

C++QtType ConversionQstring

C++ Problem Overview


I am trying to do something like this:

QString string;
// do things...
std::cout << string << std::endl;

but the code doesn't compile. How to output the content of qstring into the console (e.g. for debugging purposes or other reasons)? How to convert QString to std::string?

C++ Solutions


Solution 1 - C++

You can use:

QString qs;
// do things
std::cout << qs.toStdString() << std::endl;

It internally uses QString::toUtf8() function to create std::string, so it's Unicode safe as well. Here's reference documentation for QString.

Solution 2 - C++

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.

So the best would be either:

QString qs;

// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();

// or this if you're on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();

The suggested (accepted) method may work if you specify codec.

See: http://doc.qt.io/qt-5/qstring.html#toLatin1

Solution 3 - C++

If your ultimate aim is to get debugging messages to the console, you can use qDebug().

You can use like,

qDebug()<<string; which will print the contents to the console.

This way is better than converting it into std::string just for the sake of debugging messages.

Solution 4 - C++

QString qstr;
std::string str = qstr.toStdString();

However, if you're using Qt:

QTextStream out(stdout);
out << qstr;

Solution 5 - C++

Best thing to do would be to overload operator<< yourself, so that QString can be passed as a type to any library expecting an output-able type.

std::ostream& operator<<(std::ostream& str, const QString& string) {
    return str << string.toStdString();
}

Solution 6 - C++

An alternative to the proposed:

QString qs;
std::string current_locale_text = qs.toLocal8Bit().constData();

could be:

QString qs;
std::string current_locale_text = qPrintable(qs);

See qPrintable documentation, a macro delivering a const char * from QtGlobal.

Solution 7 - C++

The simplest way would be QString::toStdString().

Solution 8 - C++

You can use this;

QString data;
data.toStdString().c_str();

Solution 9 - C++

 QString data;
   data.toStdString().c_str();

could even throw exception on VS2017 compiler in xstring

 ~basic_string() _NOEXCEPT
        {	// destroy the string
	    _Tidy_deallocate();
	    }

the right way ( secure - no exception) is how is explained above from Artyom

 QString qs;

    // Either this if you use UTF-8 anywhere
    std::string utf8_text = qs.toUtf8().constData();

    // or this if you're on Windows :-)
    std::string current_locale_text = qs.toLocal8Bit().constData();

Solution 10 - C++

Try this:

#include <QDebug>
QString string;
// do things...
qDebug() << "right" << string << std::endl;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaugustinView Question on Stackoverflow
Solution 1 - C++Pablo Santa CruzView Answer on Stackoverflow
Solution 2 - C++ArtyomView Answer on Stackoverflow
Solution 3 - C++liaKView Answer on Stackoverflow
Solution 4 - C++chrisView Answer on Stackoverflow
Solution 5 - C++PuppyView Answer on Stackoverflow
Solution 6 - C++flokkView Answer on Stackoverflow
Solution 7 - C++shaveenkView Answer on Stackoverflow
Solution 8 - C++Hafsa Elif ÖzçiftciView Answer on Stackoverflow
Solution 9 - C++JPMView Answer on Stackoverflow
Solution 10 - C++wudongliangView Answer on Stackoverflow