From 29adc0eed9b9dc3cce92f8acaaeaed3ab1bc6414 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 5 Dec 2019 16:45:31 +0100 Subject: Fix updating the text cursor position after editing In some cases when editing the text (for example when removing the selected text, or pasting a text block) the text cursor position is updated, but its visual x position is not updated. This causes the next cursor movements to start from a wrong position. Force the update for those cases. Fixes: QTBUG-78479 Change-Id: Ia496be62beec58660f5e1695e5aafae09c79684e Reviewed-by: Christian Ehrlicher --- .../widgets/qplaintextedit/tst_qplaintextedit.cpp | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp') diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index 2ce75620cf..2faa02e284 100644 --- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp @@ -155,6 +155,7 @@ private slots: #if QT_CONFIG(scrollbar) void updateAfterChangeCenterOnScroll(); #endif + void updateCursorPositionAfterEdit(); private: void createSelection(); @@ -1789,5 +1790,50 @@ void tst_QPlainTextEdit::updateAfterChangeCenterOnScroll() #endif +void tst_QPlainTextEdit::updateCursorPositionAfterEdit() +{ + QPlainTextEdit plaintextEdit; + plaintextEdit.setPlainText("some text some text\nsome text some text"); + + const auto initialPosition = 1; + + // select some text + auto cursor = plaintextEdit.textCursor(); + cursor.setPosition(initialPosition, QTextCursor::MoveAnchor); + cursor.setPosition(initialPosition + 3, QTextCursor::KeepAnchor); + plaintextEdit.setTextCursor(cursor); + QVERIFY(plaintextEdit.textCursor().hasSelection()); + + QTest::keyClick(&plaintextEdit, Qt::Key_Delete); + QTest::keyClick(&plaintextEdit, Qt::Key_Down); + QTest::keyClick(&plaintextEdit, Qt::Key_Up); + + // Moving the cursor down and up should bring it to the initial position + QCOMPARE(plaintextEdit.textCursor().position(), initialPosition); + + // Test the same with backspace + cursor = plaintextEdit.textCursor(); + cursor.setPosition(initialPosition + 3, QTextCursor::KeepAnchor); + plaintextEdit.setTextCursor(cursor); + QVERIFY(plaintextEdit.textCursor().hasSelection()); + + QTest::keyClick(&plaintextEdit, Qt::Key_Backspace); + QTest::keyClick(&plaintextEdit, Qt::Key_Down); + QTest::keyClick(&plaintextEdit, Qt::Key_Up); + + // Moving the cursor down and up should bring it to the initial position + QCOMPARE(plaintextEdit.textCursor().position(), initialPosition); + + // Test insertion + const QString txt("txt"); + QApplication::clipboard()->setText(txt); + plaintextEdit.paste(); + QTest::keyClick(&plaintextEdit, Qt::Key_Down); + QTest::keyClick(&plaintextEdit, Qt::Key_Up); + + // The curser should move back to the end of the copied text + QCOMPARE(plaintextEdit.textCursor().position(), initialPosition + txt.length()); +} + QTEST_MAIN(tst_QPlainTextEdit) #include "tst_qplaintextedit.moc" -- cgit v1.2.3