diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index bd7f844eca..8f0fff3d5c 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -289,7 +289,7 @@ void QPlainTextDocumentLayout::documentChanged(int from, int charsRemoved, int c Q_D(QPlainTextDocumentLayout); QTextDocument *doc = document(); int newBlockCount = doc->blockCount(); - int charsChanged = qMax(charsRemoved, charsAdded); + int charsChanged = charsRemoved + charsAdded; QTextBlock changeStartBlock = doc->findBlock(from); QTextBlock changeEndBlock = doc->findBlock(qMax(0, from + charsChanged - 1)); diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index c47f7b1ff6..613d4fa28c 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: void findWithRegExpReturnsFalseIfNoMoreResults(); #endif void layoutAfterMultiLineRemove(); + void undoCommandRemovesAndReinsertsBlock(); private: void createSelection(); @@ -1612,5 +1613,29 @@ void tst_QPlainTextEdit::layoutAfterMultiLineRemove() QCOMPARE(curs.blockNumber(), 3); } +void tst_QPlainTextEdit::undoCommandRemovesAndReinsertsBlock() +{ + ed->setVisible(true); + ed->setPlainText(QStringLiteral("line1\nline2")); + QCOMPARE(ed->document()->blockCount(), 2); + + QTextCursor cursor = ed->textCursor(); + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); + cursor.insertText(QStringLiteral("\n")); + QCOMPARE(ed->document()->blockCount(), 2); + + ed->undo(); + QCOMPARE(ed->document()->blockCount(), 2); + + QTextBlock block; + for (block = ed->document()->begin(); block != ed->document()->end(); block = block.next()) { + QVERIFY(block.isValid()); + QCOMPARE(block.length(), 6); + QVERIFY(block.layout()->lineForTextPosition(0).isValid()); + } + +} + QTEST_MAIN(tst_QPlainTextEdit) #include "tst_qplaintextedit.moc"