summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2019-12-05 16:45:31 +0100
committerSona Kurazyan <sona.kurazyan@qt.io>2019-12-06 15:52:35 +0100
commit29adc0eed9b9dc3cce92f8acaaeaed3ab1bc6414 (patch)
tree39037a48dcd31e7df105a96ab536e5e0149c28c3
parentca7033935a26e2a11c7d872a6673e828eddf49ca (diff)
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 <ch.ehrlicher@gmx.de>
-rw-r--r--src/gui/text/qtextcursor.cpp1
-rw-r--r--src/widgets/widgets/qwidgettextcontrol.cpp6
-rw-r--r--tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp46
3 files changed, 53 insertions, 0 deletions
diff --git a/src/gui/text/qtextcursor.cpp b/src/gui/text/qtextcursor.cpp
index c88497840f..056a854789 100644
--- a/src/gui/text/qtextcursor.cpp
+++ b/src/gui/text/qtextcursor.cpp
@@ -2261,6 +2261,7 @@ void QTextCursor::insertFragment(const QTextDocumentFragment &fragment)
d->remove();
fragment.d->insert(*this);
d->priv->endEditBlock();
+ d->setX();
if (fragment.d && fragment.d->doc)
d->priv->mergeCachedResources(fragment.d->doc->docHandle());
diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp
index 1c169c3325..83e2315c36 100644
--- a/src/widgets/widgets/qwidgettextcontrol.cpp
+++ b/src/widgets/widgets/qwidgettextcontrol.cpp
@@ -1283,6 +1283,8 @@ void QWidgetTextControlPrivate::keyPressEvent(QKeyEvent *e)
} else {
QTextCursor localCursor = cursor;
localCursor.deletePreviousChar();
+ if (cursor.d)
+ cursor.d->setX();
}
goto accept;
}
@@ -1322,9 +1324,13 @@ void QWidgetTextControlPrivate::keyPressEvent(QKeyEvent *e)
else if (e == QKeySequence::Delete) {
QTextCursor localCursor = cursor;
localCursor.deleteChar();
+ if (cursor.d)
+ cursor.d->setX();
} else if (e == QKeySequence::Backspace) {
QTextCursor localCursor = cursor;
localCursor.deletePreviousChar();
+ if (cursor.d)
+ cursor.d->setX();
}else if (e == QKeySequence::DeleteEndOfWord) {
if (!cursor.hasSelection())
cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
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"