Search code examples
c++qt5qtextedit

Is there a simple way to change the "text changed" status in QTextEdit?


I need to verify my source file and even omit some "service" lines, so I do it using appendPlainText() of QPlainTextEdit. Appending a line of course means a change,so after loading the file, the asterisk meaning that the file changed appears. I would like to have the more consistent behavior, that after loading, this status signal is not set. How can I reset it, after I loaded the file?


Solution

  • You can surround the part of the code that emits the unwanted signal by two QObject::blockSignals calls:

    textEdit->blockSignals(true);
    // load from file
    textEdit->blockSignals(false);
    

    or directly on QTextEdit::document (will block fewer other signals, I suppose):

    textEdit->document()->blockSignals(true);
    // load from file
    textEdit->document()->blockSignals(false);
    

    Maybe even call QTextEdit::setModified immediately after loading (two signals will be emitted).

    Try each one of these out and give me know if any of them doesn't work.