Add QTextDocument::toRawText() function

The QTextDocument::toPlainText() converts some characters in the
text to ASCII, which can be problematic for use cases where you
want to save the precise contents of the document, e.g. in
Qt Creator. Since we don't want to change the behavior of
toPlainText(), we introduce a new function which returns the
raw text contents of the document instead, with no modifications.

[ChangeLog][QtGui][Text] Added QTextDocument::toRawText() function.

Task-number: QTBUG-56538
Change-Id: Ib6c48a16551c4c71c4c431760f993793d1af6806
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Eskil Abrahamsen Blomfeldt 2016-10-14 15:54:13 +02:00
parent 9944a12a0f
commit ae6681673e
3 changed files with 31 additions and 0 deletions

View File

@ -1144,10 +1144,29 @@ void QTextDocument::setMetaInformation(MetaInformation info, const QString &stri
}
}
/*!
Returns the raw text contained in the document without any
formatting information. If you want formatting information
use a QTextCursor instead.
\since 5.9
\sa toPlainText()
*/
QString QTextDocument::toRawText() const
{
Q_D(const QTextDocument);
return d->plainText();
}
/*!
Returns the plain text contained in the document. If you want
formatting information use a QTextCursor instead.
This function returns the same as toRawText(), but will replace
some unicode characters, such as line separators and non-breaking
spaces, with ASCII alternatives. If you need the precise contents
of the document, use toRawText() instead.
\note Embedded objects, such as images, are represented by a
Unicode value U+FFFC (OBJECT REPLACEMENT CHARACTER).

View File

@ -151,6 +151,7 @@ public:
void setHtml(const QString &html);
#endif
QString toRawText() const;
QString toPlainText() const;
void setPlainText(const QString &text);

View File

@ -135,6 +135,7 @@ private slots:
void setPlainText();
void toPlainText();
void toRawText();
void deleteTextObjectsOnClear();
@ -2396,6 +2397,16 @@ void tst_QTextDocument::toPlainText()
QCOMPARE(doc->toPlainText(), QLatin1String("Hello World"));
}
void tst_QTextDocument::toRawText()
{
doc->setHtml("&nbsp;");
QString rawText = doc->toRawText();
QCOMPARE(rawText.size(), 1);
QCOMPARE(rawText.at(0).unicode(), ushort(QChar::Nbsp));
}
void tst_QTextDocument::deleteTextObjectsOnClear()
{
QPointer<QTextTable> table = cursor.insertTable(2, 2);