admin管理员组文章数量:1391929
I have a Qt C++ app where I need to save the contents of a QTextEdit to disk as plain text if it contains plain text, and as html if the QTextEdit contains rich text.
Is there any way to tell if its contents are rich text or plain text?
In the case of plain text, that would mean its contents has no formatting, and all text has no specified font or is using the default font.
I have a Qt C++ app where I need to save the contents of a QTextEdit to disk as plain text if it contains plain text, and as html if the QTextEdit contains rich text.
Is there any way to tell if its contents are rich text or plain text?
In the case of plain text, that would mean its contents has no formatting, and all text has no specified font or is using the default font.
Share Improve this question edited Mar 21 at 8:00 Pamputt 4443 silver badges14 bronze badges asked Mar 12 at 18:38 Bri BriBri Bri 2,0723 gold badges24 silver badges65 bronze badges 5 |2 Answers
Reset to default 3There's no way to distinguish, since QTextDocument
doesn't track that information. The following two calls produce exactly the same state in the document:
QTextDocument doc;
doc.setPlainText("lorem ipsum\ndolor sit amet\nconsectetur adipiscing elit");
doc.setHtml("<p style=\"margin-top:0;margin-bottom:0;\">lorem ipsum</p>"
"<p style=\"margin-top:0;margin-bottom:0;\">dolor sit amet</p>"
"<p style=\"margin-top:0;margin-bottom:0;\">consectetur adipiscing elit</p>");
Because of that, the best solution is to maintain a separate variable to record which format we have.
If we can't (or won't) to do that, there's a reasonably good heuristic we can use - if we convert the plain text to HTML, do we get the same result as HTML direct from the document?
That test looks like this:
#include <QTextDocument>
bool isPlainText(QTextDocument const *doc)
{
return QTextDocument{doc->toRawText()}.toHtml() == doc->toHtml();
}
Demo:
int main()
{
QTextDocument doc;
doc.setPlainText("lorem ipsum\ndolor sit amet\nconsectetur adipiscing elit");
assert(isPlainText(&doc));
doc.setHtml("<p>lorem ipsum</p>"
"<p>dolor sit amet</p>"
"<p>consectetur adipiscing elit</p>");
assert(!isPlainText(&doc));
}
I suggest to use the Qt::mightBeRichText function.
As the official Qt documentation states:
Returns
true
if the string text is likely to be rich text; otherwise returnsfalse
.
Although it might detect if it is rich text, in my opinion it's an accurate approach.
本文标签: cIs there a way to tell if a QTextEdit contains plain text or rich textStack Overflow
版权声明:本文标题:c++ - Is there a way to tell if a QTextEdit contains plain text or rich text? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744734185a2622224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
allFormats()
returns more than one QTextCharFormat, more than one QTextFrameFormat (possibly indicating a table), or contains some QTextListFormat. Take your time to carefully study the Rich Text Document Structure. – musicamante Commented Mar 12 at 20:07