diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-10-07 23:44:44 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-10-08 15:50:54 +0000 |
commit | dfa0948e936aad3ea823427d5b015c4d906c7113 (patch) | |
tree | 2aafc807a577fda3c067d243dc4f93df60319b8f | |
parent | 2acb31641fc9c34d24ac29232cdfec2673834629 (diff) |
QQuickTextEdit: ensure we update document width when padding has changed
We use an if-test to check if the document width has changed before
we set the new value. The problem is that the value we test
against is different than the value we set. The result is that
we can sometimes skip setting a new width on the document, even
if padding has changed.
This patch ensures that we use the same width for both testing
and setting.
Change-Id: Ia8391999e8cc2b5be72fe525d396bf8c17ba0fa2
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
(cherry picked from commit 40993321cd67c1fe722977ed94c91cedff4bb1f8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/quick/items/qquicktextedit.cpp | 5 | ||||
-rw-r--r-- | tests/auto/quick/qquicktextedit/data/wordwrap.qml | 8 | ||||
-rw-r--r-- | tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp | 25 |
3 files changed, 36 insertions, 2 deletions
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index 247d8abc3c..068571f15e 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -2530,8 +2530,9 @@ void QQuickTextEdit::updateSize() if (d->inLayout) // probably the result of a binding loop, but by letting it return; // get this far we'll get a warning to that effect. } - if (d->document->textWidth() != width()) { - d->document->setTextWidth(width() - leftPadding() - rightPadding()); + const qreal newTextWidth = width() - leftPadding() - rightPadding(); + if (d->document->textWidth() != newTextWidth) { + d->document->setTextWidth(newTextWidth); newWidth = d->document->idealWidth(); } //### need to confirm cost of always setting these diff --git a/tests/auto/quick/qquicktextedit/data/wordwrap.qml b/tests/auto/quick/qquicktextedit/data/wordwrap.qml new file mode 100644 index 0000000000..8c356a2fef --- /dev/null +++ b/tests/auto/quick/qquicktextedit/data/wordwrap.qml @@ -0,0 +1,8 @@ +import QtQuick 2.6 + +TextEdit { + width: 200 + height: 200 + text: "X Y Z X Y Z X Y Z X Y Z X Y Z X Y Z X Y Z X Y Z" + wrapMode: TextEdit.WordWrap +} diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index 768062314b..f5637e47f1 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -217,6 +217,7 @@ private slots: void doubleSelect_QTBUG_38704(); void padding(); + void paddingAndWrap(); void QTBUG_51115_readOnlyResetsSelection(); void keys_shortcutoverride(); @@ -5770,6 +5771,30 @@ void tst_qquicktextedit::padding() delete root; } +void tst_qquicktextedit::paddingAndWrap() +{ + // Check that the document ends up with the correct width if + // we set left and right padding after component completed. + QScopedPointer<QQuickView> window(new QQuickView); + window->setSource(testFileUrl("wordwrap.qml")); + QTRY_COMPARE(window->status(), QQuickView::Ready); + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window.data())); + QQuickItem *root = window->rootObject(); + QVERIFY(root); + QQuickTextEdit *obj = qobject_cast<QQuickTextEdit *>(root); + QVERIFY(obj != nullptr); + QTextDocument *doc = QQuickTextEditPrivate::get(obj)->document; + + QCOMPARE(doc->textWidth(), obj->width()); + obj->setLeftPadding(10); + obj->setRightPadding(10); + QCOMPARE(doc->textWidth(), obj->width() - obj->leftPadding() - obj->rightPadding()); + obj->setLeftPadding(0); + obj->setRightPadding(0); + QCOMPARE(doc->textWidth(), obj->width()); +} + void tst_qquicktextedit::QTBUG_51115_readOnlyResetsSelection() { QQuickView view; |