⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QTextEdit and CR

  • 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.

  • Lifetime Qt Champion


    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.

  • 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.

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

  • You can use setPlainText()

  • Lifetime Qt Champion

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

Log in to reply