#ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include #include #include class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget * parent = nullptr) : QMainWindow(parent) { auto * edit = new QTextEdit(); this->setCentralWidget(edit); auto * doc = edit->document(); doc->setHtml("\ \
  • Text Item Test
"); connect(doc, &QTextDocument::contentsChange, this, [ = ](int pos, int removed, int added) { // the naive way of getting the doc length auto docCharLength = doc->characterCount(); // and what QCursor tells us QTextCursor cursor(doc); cursor.movePosition(QTextCursor::End); int docCursorLength = cursor.position(); /// const auto changeEnd = pos + added; qInfo() << pos << ", " << removed << ", " << added; qInfo() << "lengths: " << docCursorLength << ", " << docCharLength << ", requires: " << changeEnd; Q_ASSERT(docCursorLength >= changeEnd); Q_ASSERT(docCharLength >= changeEnd); }); } }; #endif // MAINWINDOW_H