QTextDocument 吃掉多个空格
有一个 Qt/C++ 代码:
#include <QtCore/QCoreApplication>
#include <QtGui/QTextDocument>
#include <QByteArray>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextDocument *doc = new QTextDocument();
qDebug() << " === Document was: === ";
qDebug() << doc->toHtml(QByteArray());
doc->setHtml("<p>THIS IS SPARTA</p>");
qDebug() << " === Document now: === ";
qDebug() << doc->toHtml(QByteArray());
return a.exec();
}
它输出:
=== Document was: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></td></tr></table></body></html>"
=== Document now: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">THIS IS SPARTA</p></td></tr></table></body></html>"
正如我所看到的,QTextDocument 中有类似默认 CSS 的内容:
<style type="text/css">p, li {white-space: pre-wrap;}</style>
但是,当我使用 p 标签和多个空格设置 HTML 时,它会删除空格。问题是——为什么? 另一个问题是 - 为什么要为 p 标签添加边距?
PS 如果我在执行 setHtml 之前添加一行,它就可以正常工作
doc->setDefaultStyleSheet("p, li { white-space: pre-wrap; }");
- 它不会删除多个空格。但这个样式标签是什么?这不是默认样式表吗?为什么Qt会忽略它?
感谢您的回答。
There is a Qt/C++ code:
#include <QtCore/QCoreApplication>
#include <QtGui/QTextDocument>
#include <QByteArray>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextDocument *doc = new QTextDocument();
qDebug() << " === Document was: === ";
qDebug() << doc->toHtml(QByteArray());
doc->setHtml("<p>THIS IS SPARTA</p>");
qDebug() << " === Document now: === ";
qDebug() << doc->toHtml(QByteArray());
return a.exec();
}
It outputs:
=== Document was: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></td></tr></table></body></html>"
=== Document now: ===
"<!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:'Helvetica'; font-size:12pt; font-weight:400; font-style:normal;">
<table style="-qt-table-type: root; margin-top:4px; margin-bottom:4px; margin-left:4px; margin-right:4px;">
<tr>
<td style="border: none;">
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">THIS IS SPARTA</p></td></tr></table></body></html>"
As I can see, there is something like default CSS in QTextDocument:
<style type="text/css">p, li {white-space: pre-wrap;}</style>
But, when I'm setting HTML with p tag and multiple whitespaces, it removes whitespaces. The question is - why?
Another question is - why is it adding margins to p tag?
PS It works fine if I add a line
doc->setDefaultStyleSheet("p, li { white-space: pre-wrap; }");
before doing setHtml - it doesn't remove multiple whitespaces. But what is this style-tag then? Isn't it a default style sheet? Why Qt ignores it?
Thanks for the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种行为来自 html,html 只是忽略标签内的多个空格..
尝试使用空格的特殊代码:
this behavior is from html, html just ignore more than one spaces inside tags..
try to use the special code for space:
该文档告诉您富文本对象中支持哪些 CSS 标签 - 空白:应该支持预包装,至少在我拥有的 Qt 4.7 文档中是这样。也许您应该在错误跟踪器中提出错误?
The documentation tells you which CSS tags are supported in a Rich Text object - white-space: pre-wrap is supposed to be supported, at least in the Qt 4.7 docs I have. Perhaps you should raise a bug in the bug tracker?