Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream write to QString ignore endl
Forum Updated to NodeBB v4.3 + New Features

QTextStream write to QString ignore endl

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 282 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    semicloud
    wrote on last edited by
    #1

    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
    3
    

    But it is seems that QTextStream totally ignore the endl manipulator. The program output is :

    1\n2\n3
    

    Besides 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?

    JKSHJ 1 Reply Last reply
    1
    • S semicloud

      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
      3
      

      But it is seems that QTextStream totally ignore the endl manipulator. The program output is :

      1\n2\n3
      

      Besides 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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      Hi, and welcome!

      @semicloud said in QTextStream write to QString ignore endl:

      1\n2\n3

      That is correct. \n is the string value of endl. This is the same debug output as qDebug() << QString("1\n2\n3");.

      What I want is as follows :

      1
      2
      3
      

      You can do one of the following:

      • Write msg to a text file and open it with a text editor.
      • Put msg in a QLabel or QTextEdit.
      • Convert msg from a QString to a const char*: qDebug() << msg.toUtf8().constData();

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved