Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to add newline to a QString?

How to add newline to a QString?

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 6 Posters 57.2k Views
  • 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.
  • M Offline
    M Offline
    mimimeow
    wrote on 8 Aug 2012, 22:43 last edited by
    #1

    I have an error message dialog where I need to pass in a QString.
    I want to break up the string into multiple lines. If I add "\n" it didn't work, it's simply ignored.
    How can I break up the long string into several lines? TIA for your help.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      codenode
      wrote on 8 Aug 2012, 23:35 last edited by
      #2

      Well, \n should work, they should patch Qt on this one afaik ;)

      But, as far as I remember, </br> could do the trick, as the contents in a QMessageBox is rendered as HTML like string.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on 9 Aug 2012, 05:19 last edited by
        #3

        Yes , "\n" works with QString , you can try the following code

        @ QMessageBox::critical(this,"error","This \n is \n an \n error \n string !!!!!");@

        that shows an error dialog like :

        !http://img814.imageshack.us/img814/6962/capturelaa.png(error!!!)!

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BrianDavis
          wrote on 11 Dec 2015, 17:35 last edited by
          #4

          \n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
          \n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text

          J 1 Reply Last reply 12 May 2022, 10:05
          7
          • B BrianDavis
            11 Dec 2015, 17:35

            \n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
            \n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text

            J Offline
            J Offline
            Jimmy Crab
            wrote on 12 May 2022, 10:05 last edited by
            #5

            @BrianDavis said in How to add newline to a QString?:

            \n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
            \n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text

            \n works. Thanks @BrianDavis for sharing.
            My code:

            QStringList strs;
                strs.append("Toyota");
                strs.append("BUICK");
                strs.append("VW");
                strs.append("Mazda");
            
                QFile p("car.txt");
                if (p.open(QFile::WriteOnly | QIODevice::Text)) {
                    QTextStream s(&p);
                    for(QString line : strs){
                        s << line << "\n";
                    }
                }
            

            2022-05-12_18-04-12.png

            Just do it.

            S 1 Reply Last reply 12 May 2022, 20:16
            0
            • J Jimmy Crab
              12 May 2022, 10:05

              @BrianDavis said in How to add newline to a QString?:

              \n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
              \n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text

              \n works. Thanks @BrianDavis for sharing.
              My code:

              QStringList strs;
                  strs.append("Toyota");
                  strs.append("BUICK");
                  strs.append("VW");
                  strs.append("Mazda");
              
                  QFile p("car.txt");
                  if (p.open(QFile::WriteOnly | QIODevice::Text)) {
                      QTextStream s(&p);
                      for(QString line : strs){
                          s << line << "\n";
                      }
                  }
              

              2022-05-12_18-04-12.png

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 12 May 2022, 20:16 last edited by
              #6

              @Jimmy-Crab hi and welcome to devnet,

              QStringList::join would be simpler.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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