From 4cf08f0757ad3e651bb090c6e449ca00b242cb93 Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Date: Thu, 9 Nov 2023 14:05:22 +0100 Subject: [PATCH] Fix polish issue in quick text edit The text edit didn't polish even after there is change in position offset, leading to alignment issues. During geometry change, the QQuickTextEdit calculates implicit size and base position offset, and then further requests a polish immediately afterwards. This sequence of update happens only if we have valid width or height set for text edit control. But in case we set width or height to undefined, there is a chance that this update would be missed. This patch removes the checking of widthValid() in QQuickTextEdit::geometryChange(), to allow those updates. The validation of width or height is already handled internally within updateSize(), so it shouldn't create an issue; and we still try to avoid emitting cursorRectangleChanged() too often. Amends 1770fa632facf2f1e4bb05e7689efc939d46cfef Task-number: QTBUG-117667 Task-number: QTBUG-25489 Pick-to: 6.5 Change-Id: Ia20cd06e78842f5edb0c395d6322a660f86f6b5e Reviewed-by: Shawn Rutledge (cherry picked from commit d84c1304112d2198a12e647fd44f57ee0e8dc437) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 3bff953242758365d81e278a6ee0f95f2deb6f1d) Reviewed-by: Qt CI Bot --- src/quick/items/qquicktextedit.cpp | 8 ++-- .../data/resizeTextEditPolish.qml | 42 +++++++++++++++++++ .../qquicktextedit/tst_qquicktextedit.cpp | 31 ++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/auto/quick/qquicktextedit/data/resizeTextEditPolish.qml diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index df83d2abf8..50e9d1febd 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1527,14 +1527,14 @@ void QQuickTextEdit::setInputMethodHints(Qt::InputMethodHints hints) void QQuickTextEdit::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) { Q_D(QQuickTextEdit); - if (!d->inLayout && ((newGeometry.width() != oldGeometry.width() && widthValid()) - || (newGeometry.height() != oldGeometry.height() && heightValid()))) { + if (!d->inLayout && ((newGeometry.width() != oldGeometry.width()) + || (newGeometry.height() != oldGeometry.height()))) { updateSize(); updateWholeDocument(); - moveCursorDelegate(); + if (widthValid() || heightValid()) + moveCursorDelegate(); } QQuickImplicitSizeItem::geometryChange(newGeometry, oldGeometry); - } void QQuickTextEdit::itemChange(ItemChange change, const ItemChangeData &value) diff --git a/tests/auto/quick/qquicktextedit/data/resizeTextEditPolish.qml b/tests/auto/quick/qquicktextedit/data/resizeTextEditPolish.qml new file mode 100644 index 0000000000..44819a0de9 --- /dev/null +++ b/tests/auto/quick/qquicktextedit/data/resizeTextEditPolish.qml @@ -0,0 +1,42 @@ +import QtQuick +import QtQuick.Controls + +Item { + width: 400 + height: 150 + + TextEdit { + id: textEdit + anchors { + top: parent.top + left: parent.left + right: parent.right + } + readOnly: true + wrapMode: Text.WordWrap + verticalAlignment: Text.AlignVCenter + text: "Lorem ipsum dolor sit amet, consectetur adipisicing" + states: [ + State { + name: "multi-line" + when: textEdit.lineCount > 1 + AnchorChanges { + target: textEdit + anchors.bottom: undefined + } + PropertyChanges { + target: textEdit + height: undefined + } + }, + State { + name: "single-line" + when: true + AnchorChanges { + target: textEdit + anchors.bottom: { return textEdit.parent.bottom } + } + } + ] + } +} diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index 60153a8312..99f95cc5a6 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -211,6 +212,7 @@ private slots: void rtlAlignmentInColumnLayout_QTBUG_112858(); + void resizeTextEditPolish(); private: void simulateKeys(QWindow *window, const QList &keys); #if QT_CONFIG(shortcut) @@ -6591,6 +6593,35 @@ void tst_qquicktextedit::rtlAlignmentInColumnLayout_QTBUG_112858() } } +void tst_qquicktextedit::resizeTextEditPolish() +{ + QQuickView window(testFileUrl("resizeTextEditPolish.qml")); + QVERIFY(window.rootObject() != nullptr); + + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + + auto *edit = window.rootObject()->findChild(); + QVERIFY(edit != nullptr); + QCOMPARE(edit->lineCount(), 1); + + QSignalSpy spy(edit, SIGNAL(lineCountChanged())); + + // Resize item and check for item polished + auto *item = edit->parentItem(); + item->setWidth(item->width() - (item->width() / 2)); + + QVERIFY(QQuickTest::qIsPolishScheduled(edit)); + QVERIFY(QQuickTest::qWaitForPolish(edit)); + + QTRY_COMPARE(spy.size(), 1); + QVERIFY(edit->lineCount() > 1); + QCOMPARE(edit->state(), QString("multi-line")); + auto *editPriv = QQuickTextEditPrivate::get(edit); + QCOMPARE(editPriv->xoff, 0); + QCOMPARE(editPriv->yoff, 0); +} + QTEST_MAIN(tst_qquicktextedit) #include "tst_qquicktextedit.moc"