diff --git a/src/gui/text/qtextcursor.cpp b/src/gui/text/qtextcursor.cpp index ac9762b183..731b6adde8 100644 --- a/src/gui/text/qtextcursor.cpp +++ b/src/gui/text/qtextcursor.cpp @@ -132,7 +132,7 @@ QTextCursorPrivate::AdjustResult QTextCursorPrivate::adjustPosition(int position void QTextCursorPrivate::setX() { - if (priv->isInEditBlock()) { + if (priv->isInEditBlock() || priv->inContentsChange) { x = -1; // mark dirty return; } diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 53aef40df0..59c951332e 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -192,6 +192,8 @@ private slots: void QTBUG28998_linkColor(); + void textCursorUsageWithinContentsChange(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); @@ -3021,5 +3023,50 @@ void tst_QTextDocument::QTBUG28998_linkColor() QCOMPARE(format.foreground(), pal.link()); } +class ContentsChangeHandler : public QObject +{ + Q_OBJECT +public: + ContentsChangeHandler(QTextDocument *doc) + : verticalMovementX(-1) + , doc(doc) + { + connect(doc, SIGNAL(contentsChange(int,int,int)), + this, SLOT(saveModifiedText(int, int, int))); + } + +private slots: + void saveModifiedText(int from, int /*charsRemoved*/, int charsAdded) + { + QTextCursor tmp(doc); + tmp.setPosition(from); + tmp.setPosition(from + charsAdded, QTextCursor::KeepAnchor); + text = tmp.selectedText(); + verticalMovementX = tmp.verticalMovementX(); + } + +public: + QString text; + int verticalMovementX; +private: + QTextDocument *doc; +}; + +void tst_QTextDocument::textCursorUsageWithinContentsChange() +{ + // force creation of layout + doc->documentLayout(); + + QTextCursor cursor(doc); + cursor.insertText("initial text"); + + ContentsChangeHandler handler(doc); + + cursor.insertText("new text"); + + QCOMPARE(handler.text, QString("new text")); + QCOMPARE(handler.verticalMovementX, -1); +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc"