Qt QTextEdit adds spurious line
Asked Answered
F

1

6

Look at this tiny piece of Qt code

qDebug() << "CONTENT" << content;

QTextEdit *te = new QTextEdit(this);
te->setHtml(content);

qDebug() << "CONTENT AFTER " << te->toHtml();

Content initially contains this HTML

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">
<html>
    <head>
        <meta name=\"qrichtext\" content=\"1\" />
        <style type=\"text/css\"> p, li { white-space: pre-wrap; } </style>
    </head>
    <body style=\" font-family:'Calibri'; font-size:10pt; font-weight:400; font-style:normal;\">
        <p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
            <span style=\" font-family:'Verdana'; color:#0b333c;\">MY TEXT</span>
        </p>
    </body>
</html>"

... but the output of te->toHtml() is...

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">
<html>
    <head>
        <meta name=\"qrichtext\" content=\"1\" />
        <style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style>
    </head>
    <body style=\" font-family:'Calibri'; font-size:10pt; font-weight:400; font-style:normal;\">
        <p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"> </p>
        <p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
            <span style=\" font-family:'Verdana'; color:#0b333c;\">MY TEXT</span>
        </p>
    </body>
</html>

As you can see, QTextEdit prepends paragraph with no reason. This is very annoying and I need absolutely to avoid it.

Any ideas? I really can't figure out how to get rid of this behaviour. Could it be a bug?

Fahlband answered 10/11, 2016 at 14:9 Comment(1)
QTextEdit is not a html editor. Methods setHtml() and toHtml() are provided just for convenience and QTextEdit do not provide warranty that you will get the exactly same thing from toHtml().Pyrometallurgy
V
0

Probably a bit late, but I've faced a similar problem several times. What I've found is that the HTML parser used by Qt doesn't like HTML indentation, and interprets such spaces as text-to-be-formatted, instead of omitting it.

To solve the issue, just remove indentation before setting the HTML.

Here a complete test (code can also be found here)

#include <qapplication.h>
#include <qtextedit.h>
#include <qdebug.h>
#include <qregexp.h>

int main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  const QString content =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">"
    "<html>"
    "    <head>"
    "        <meta name=\"qrichtext\" content=\"1\" />"
    "        <style type=\"text/css\"> p, li { white-space: pre-wrap; } </style>"
    "    </head>"
    "    <body style=\" font-family:'Calibri'; font-size:10pt; font-weight:400; font-style:normal;\">"
    "        <p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
    "            <span style=\" font-family:'Verdana'; color:#0b333c;\">MY TEXT</span>"
    "        </p>"
    "    </body>"
    "</html>";

  qDebug() << "Original content";
  qDebug() << content;

  QTextEdit text_edit_1;
  text_edit_1.setHtml(content);
  qDebug() << "";
  qDebug() << "HTML from QTextEdit:";
  qDebug() << text_edit_1.toHtml();

  // Removing spaces (not necessarily the best way, but it works for this example)
  const QString content2 = QString(content).replace(QRegExp("\\s+<"), "<");
  qDebug() << "";
  qDebug() << "Content without spaces:";
  qDebug() << content2;

  QTextEdit text_edit_2;
  text_edit_2.setHtml(content2);
  qDebug() << "";
  qDebug() << "HTML from QTextEdit:";
  qDebug() << text_edit_2.toHtml();

  QTextEdit text_edit_3;
  text_edit_3.setHtml(text_edit_2.toHtml());
  qDebug() << "";
  qDebug() << "After re-using HTML:";
  qDebug() << text_edit_3.toHtml();

  return 0;
}

I've tested it using Qt 5.1.0, and the results are the following (I've only pretty-formatted the output HTML to ease the inspection):

Result with original content

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html>
  <head>
    <meta name=\"qrichtext\" content=\"1\" />
    <style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style>
  </head>
  <body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">        </p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
      <span style=\" font-family:'Calibri'; font-size:10pt;\">        </span>
    </p>\n<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
      <span style=\" font-family:'Calibri'; font-size:10pt;\">            </span>
      <span style=\" font-family:'Verdana'; font-size:10pt; color:#0b333c;\">MY TEXT</span>
      <span style=\" font-family:'Calibri'; font-size:10pt;\">            </span>
    </p>
  </body>
</html>

Result after removing indentation and spaces between tags from content

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html>
  <head>
    <meta name=\"qrichtext\" content=\"1\" />
    <style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style>
  </head>
  <body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
      <span style=\" font-family:'Verdana'; font-size:10pt; color:#0b333c;\">MY TEXT</span>
    </p>
  </body>
</html>

Results using HTML generated by QTextEdit::toHtml()

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html>
  <head>
    <meta name=\"qrichtext\" content=\"1\" />
    <style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style>
  </head>
  <body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">
      <span style=\" font-family:'Verdana'; font-size:10pt; color:#0b333c;\">MY TEXT</span>
    </p>
  </body>
</html>

As it can be seen too, the HTML produced by QTextEdit can be re-inserted into the document without adding the spurious lines.

As a note, all of this also applies to QTextDocument, which is basically the under-the-hood structure used by QTextEdit.

Vibration answered 14/9, 2018 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.