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. How to insert a newline character in QString and display it using qDebug()?
Forum Updated to NodeBB v4.3 + New Features

How to insert a newline character in QString and display it using qDebug()?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 13.9k Views 1 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.
  • K Offline
    K Offline
    kahlenberg
    wrote on 20 Mar 2016, 15:25 last edited by
    #1

    Hi,
    I want to insert a '-' char at every 4th Position in a QString, and a newline at every 80th Position to Display it with qDebug().
    The following code does not work, it insersts "\n" as normal string. I tried QChar('\n'), only '\n' or "\n" but without success.
    How to do that?

    Thanks.

        QString reply = QString(CommandsList[i].Reply.toHex()).toUpper(); // reply contains hex characters
        for(unsigned j=4;j < reply.size(); j=j+5)
        {
    
            if(j%79==0)
                reply.insert(j, QString("\n"));
            else
                reply.insert(j, QChar('-'));
        }
        qDebug() << reply;
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 20 Mar 2016, 15:58 last edited by Chris Kawa
      #2

      How to do that?

      You are already doing it. The problem is not in the string. It's how you're viewing it. qDebug() is meant for debugging. It escapes the non-text characters in case you want to see what they are. If you want to see the text in a formatted way use qDebug().noquote() or the std output stream.
      For example:

      QString s = "hi\thello";
      qDebug() << s;             //prints "hi\thello"
      qDebug().noquote() << s;   //prints hi	hello
      

      Oh, and please don't use qDebug() for output in production code. It's not Qt's replacement of standard streams. What and how it outputs may change in the future. It did actually lately and it caused a major discussion on the mailing list because people were using it for regular output instead of just debugging.

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kahlenberg
        wrote on 20 Mar 2016, 16:14 last edited by
        #3

        Thank you! It works now as expected. And I use it only for debug purposes.

        1 Reply Last reply
        0

        1/3

        20 Mar 2016, 15:25

        • Login

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