Home >Backend Development >C++ >How to Convert a QString to a std::string for Output?
Converting QString to std::string
Question:
You have a QString object and want to output its contents to the console using std::cout. However, the code doesn't compile due to type mismatch. How can you convert a QString to a std::string for output?
Answer:
To convert a QString to a std::string, use the toStdString() member function:
<code class="cpp">QString qs; // do things std::cout << qs.toStdString() << std::endl;</code>
This function internally uses QString::toUtf8() to create the std::string, ensuring Unicode safety. Refer to the Qt documentation for more information on QString: https: //doc.qt.io/qt-5/qstring.html
The above is the detailed content of How to Convert a QString to a std::string for Output?. For more information, please follow other related articles on the PHP Chinese website!