From 81459dd9c05f057981ddf2db0c2302030a73cb01 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Wed, 4 Dec 2019 10:57:08 +0100 Subject: Copy formatting attributes when cloning an empty QTextDocument When cloning a QTextDocument, the text fragment of the original document is copied into the new one, which results into copying also the formatting attributes. However, when the text document is empty, the corresponding text fragment is also empty, so nothing is copied. If we want to transfer the formatting attributes for an empty document, we need to set them explicitly. Fixes: QTBUG-80399 Change-Id: I382cd0821723436120af47c06ec7bfa849636307 Reviewed-by: Cristian Maureira-Fredes Reviewed-by: Eirik Aavitsland --- .../gui/text/qtextdocument/tst_qtextdocument.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp') diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 52e56feb5a..33d4976b2a 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -125,6 +125,7 @@ private slots: void clonePreservesResources(); void clonePreservesUserStates(); void clonePreservesIndentWidth(); + void clonePreservesFormatsWhenEmpty(); void blockCount(); void defaultStyleSheet(); @@ -2342,6 +2343,32 @@ void tst_QTextDocument::clonePreservesIndentWidth() delete clone; } +void tst_QTextDocument::clonePreservesFormatsWhenEmpty() +{ + QTextDocument document; + QTextCursor cursor(&document); + + // Change a few char format attributes + QTextCharFormat charFormat; + charFormat.setFontPointSize(charFormat.fontPointSize() + 1); + charFormat.setFontWeight(charFormat.fontWeight() + 1); + cursor.setBlockCharFormat(charFormat); + + // Change a few block format attributes + QTextBlockFormat blockFormat; + blockFormat.setAlignment(Qt::AlignRight); // The default is Qt::AlignLeft + blockFormat.setIndent(blockFormat.indent() + 1); + cursor.setBlockFormat(blockFormat); + + auto clone = document.clone(); + QTextCursor cloneCursor(clone); + + QCOMPARE(cloneCursor.blockCharFormat().fontPointSize(), charFormat.fontPointSize()); + QCOMPARE(cloneCursor.blockCharFormat().fontWeight(), charFormat.fontWeight()); + QCOMPARE(cloneCursor.blockFormat().alignment(), blockFormat.alignment()); + QCOMPARE(cloneCursor.blockFormat().indent(), blockFormat.indent()); +} + void tst_QTextDocument::blockCount() { QCOMPARE(doc->blockCount(), 1); -- cgit v1.2.3