QTextStream write to QString ignore endl
-
My program is simple
#include <QTextStream> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QString msg; QTextStream stream(&msg, QIODevice::Text | QIODevice::WriteOnly); stream << "1" << endl; stream << "2" << endl; stream << "3"; qDebug() << msg; // Output "1\n2\n3" return 0; }What I want is as follows :
1 2 3But it is seems that QTextStream totally ignore the endl manipulator. The program output is :
1\n2\n3Besides endl, I have already tried '\n', '\r\n', but not work too.
My platform is Windows 11, the Qt version is 5.12.11(MSVC2017 64bit), and the compiler is MSVC.
Any one can help?
-
My program is simple
#include <QTextStream> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QString msg; QTextStream stream(&msg, QIODevice::Text | QIODevice::WriteOnly); stream << "1" << endl; stream << "2" << endl; stream << "3"; qDebug() << msg; // Output "1\n2\n3" return 0; }What I want is as follows :
1 2 3But it is seems that QTextStream totally ignore the endl manipulator. The program output is :
1\n2\n3Besides endl, I have already tried '\n', '\r\n', but not work too.
My platform is Windows 11, the Qt version is 5.12.11(MSVC2017 64bit), and the compiler is MSVC.
Any one can help?
Hi, and welcome!
@semicloud said in QTextStream write to QString ignore endl:
1\n2\n3
That is correct.
\nis the string value ofendl. This is the same debug output asqDebug() << QString("1\n2\n3");.What I want is as follows :
1 2 3You can do one of the following:
- Write
msgto a text file and open it with a text editor. - Put
msgin a QLabel or QTextEdit. - Convert
msgfrom a QString to a const char*:qDebug() << msg.toUtf8().constData();
- Write