看看这一小块Qt代码
qDebug() << "CONTENT" << content;
QTextEdit *te = new QTextEdit(this);
te->setHtml(content);
qDebug() << "CONTENT AFTER " << te->toHtml();
Run Code Online (Sandbox Code Playgroud)
内容最初包含此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>"
Run Code Online (Sandbox Code Playgroud)
......但输出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>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,QTextEdit
没有任何理由的前言段落.这非常烦人,我绝对需要避免它.
有任何想法吗?我真的无法弄清楚如何摆脱这种行为.它可能是一个错误吗?
可能有点晚了,但我已经多次遇到类似的问题。我发现 Qt 使用的 HTML 解析器不喜欢HTML 缩进,并将此类空格解释为要格式化的文本,而不是忽略它。
要解决这个问题,只需在设置 HTML 之前删除缩进即可。
#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;
}
Run Code Online (Sandbox Code Playgroud)
我使用 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>
Run Code Online (Sandbox Code Playgroud)
从内容中删除缩进和标签之间的空格后的结果
<!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>
Run Code Online (Sandbox Code Playgroud)
使用 HTML 生成的结果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>
Run Code Online (Sandbox Code Playgroud)
也可以看出,生成的 HTMLQTextEdit
可以重新插入到文档中,而无需添加虚假行。
请注意,所有这些也适用于QTextDocument
,它基本上是 所使用的底层结构QTextEdit
。
归档时间: |
|
查看次数: |
171 次 |
最近记录: |