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. QTextEdit and CR
Qt 6.11 is out! See what's new in the release blog

QTextEdit and CR

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.1k 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.
  • R Offline
    R Offline
    RSFJ
    wrote on last edited by
    #1

    I'm having trouble filling a QTextEdit object with a series of numbers, each one of which is to appear on a separate line. For example,

    @
    QString blah;
    for (int i = 1; i < 100; i++)
    {
    blah += QString::number(i);
    blah += tr("\n");
    }

    qDebug() << blah;

    testWidget = new QTextEdit(blah);@

    The call to qDebug() prints just what you would expect: the numbers from 1 to 99, each on a different line. But the QTextEdit object displays the numbers on one long line.

    If it matters, I'm on Windows. The QTextEdit object is display with everything at the default settings.

    I tried various combinations of using (or not) tr() in the hope that it might perform some magic, and I tried "\n", "\n\r", "\r\n" and (just for kicks) "\r". They all do the same thing: the CR and/or LF becomes a space.

    This seems like the sort of thing that must have been discussed umpteen times, but I haven't found anything helpful.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      tr won't help you here, it's only there for internationalisation purposes.

      The problem lies in the fact that the constructor interprets the string has html.

      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
      0
      • R Offline
        R Offline
        RSFJ
        wrote on last edited by
        #3

        Assuming that you're not joking about the html, how are you supposed to turn that "feature" off?

        I tried QString::fromLatin1(), fromUtf8(), fromLocal8Bit(), and even the constructor QString(1,'\n'). I get the same result every time. It's a mystery why this problem (and I can't say that I understand what is going on here) hasn't arisen for me before.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RSFJ
          wrote on last edited by
          #4

          I understand what you mean now. The QString is where the problem lies. I need to call QTextEdit.setAcceptRichText(false). Thanks.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            You can use setPlainText()

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That, or you have to create a valid html string containing your series of numbers or simply use setPlainText(const QString&)

              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
              0

              • Login

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