c++ - Qt QTextEdit 添加虚假行

标签 c++ qt qtextedit

看看这一小段 Qt 代码

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

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

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

内容最初包含此 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>"

...但是 te->toHtml() 的输出是...

<!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>

如您所见,QTextEdit 无缘无故地添加了段落。这非常烦人,我绝对需要避免它。

有什么想法吗?我真的不知道如何摆脱这种行为。会不会是一个错误?

最佳答案

可能有点晚了,但我已经多次遇到类似的问题。我发现 Qt 使用的 HTML 解析器不喜欢 HTML 缩进,并将这些空格解释为要格式化的文本,而不是忽略它。

要解决此问题,只需在设置 HTML 之前删除缩进。

这里是一个完整的测试(代码也可以找到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;
}

我已经使用 Qt 5.1.0 对其进行了测试,结果如下(我只是对输出的 HTML 进行了漂亮的格式化以方便检查):

带有原创内容的结果

<!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>

从内容中删除缩进和标签之间的空格后的结果

<!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>

使用 QTextEdit::toHtml() 生成的 HTML 结果

<!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>

同样可以看出,QTextEdit 生成的 HTML 可以重新插入到文档中而无需添加虚假行。

请注意,所有这些也适用于 QTextDocument,它基本上是 QTextEdit 使用的底层结构。

关于c++ - Qt QTextEdit 添加虚假行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40529639/

相关文章:

c++ - 无法使用 std::for_each 在 QTextEdit 中构建完整的 html 表

c++ - C++ 中的字符串乘法

c++ - 在容器之间 move 一系列元素?

python - PyQt 通用科学软件模式 : Tree and Settings Box

c++ - Qt桌面着色器编译问题

qt - 从 QMediaPlayer 获取 QAudioBuffer

c++ - QT qml TextField 第一次加载时不显示光标

c++ - 我如何知道哪个上下文处于事件状态?

c++ - 使用 movdqa 从标签移动到 xmm# 给我未处理的异常

c++ - 如何使用带工具栏的 QTextEdit 实现编辑器