Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-49062

Wrong values appear in QTextDocument::contentsChange() signal

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4: Low P4: Low
    • None
    • 5.5.1
    • GUI: Text handling
    • None
    • OS X, Windows
    • macOS

      Sometimes (for example pasting in an empty QTextEdit) wrong parameter values are passed when QTextDocument::contentsChange() signal is emitted.

      #include <QtWidgets/QApplication>
      #include <QtWidgets/QTextEdit>
      
      QSharedPointer<QTextEdit> field;
      void onContentsChange(int position, int removed, int added) {
      	Q_UNUSED(removed);
      	QTextCursor c(field->document()->docHandle(), 0);
      	c.movePosition(QTextCursor::End);
      	Q_ASSERT(position + added <= c.position());
      }
      
      int main(int argc, char *argv[]) {
      	QApplication a(argc, argv);
      
      	field.reset(new QTextEdit());
      	QObject::connect(field->document(), &QTextDocument::contentsChange, onContentsChange);
      	field->setGeometry(100, 100, 150, 30);
      	field->show();
      
      	return a.exec();
      }
      

      In this example if we edit text in normal ways (print and remove characters) or paste text in the end of some not empty text inserted everything is going fine. But if we paste text in an empty field the assert triggers: we get "removed=1" (instead of 0) and "added" is larger by 1 than it should be.

      In more complicated situations (pasting instead of part of selected text) we are unable to determine the correct changed part of the QTextEdit content.

      Example project (Xcode 7) is attached.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            vgt Eirik Aavitsland
            johnpreston John Preston
            Votes:
            5 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes