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 Can you show what code you currently have? A minimal reproducible example is needed in order to help you. – Ilian Zapryanov Commented Mar 12 at 19:37
  • Can I ask why? What's the real problem you're trying to solve? As presented this comes across as an xy-problem. – G.M. Commented Mar 12 at 19:46
  • @G.M. This is not an xy-problem. I have an app that needs to save the contents of a QTextEdit to disk as plain text if the contents are plain text, and as html if the contents are rich text. It's quite straight-forward. – Bri Bri Commented Mar 12 at 19:56
  • 2 QTextDocument provides many ways to iterate through the document and check its formats. You could start by checking if the doc 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
  • 2 @BriBri Please put that information in the question itself rather than as a comment. – G.M. Commented Mar 12 at 20:08
Add a comment  | 

2 Answers 2

Reset to default 3

There'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 returns false.

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