Wrong value for ui->textEdit->document()->isModified()
-
I have created a new Qt Widgets Application project with a MainWindow class and a MainWindow.ui file. In MainWindow.ui I've added a QTextEdit widget with its objectName set to textEdit. MainWindow.cpp has the following content:
@#include "MainWindow.h"
#include "ui_MainWindow.h"#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);connect(ui->textEdit->document(), &QTextDocument::modificationChanged, this, &MainWindow::on_textEditDocumentModificationChanged); qDebug() << "before clear():" << ui->textEdit->document()->isModified(); // false qDebug() << "ui->textEdit->clear():"; ui->textEdit->clear(); qDebug() << "after clear():" << ui->textEdit->document()->isModified() << // false " [Why here it's false?]";
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_textEditDocumentModificationChanged(bool modified)
{
qDebug() << " on_textEditDocumentModificationChanged:" <<
modified << ui->textEdit->document()->isModified(); // true true
}@The on_textEditDocumentModificationChanged slot is called with a 'true' parameter and inside that slot @ui->textEdit->document()->isModified()@ has the value 'true', because calling the clear() method changes the modified status of the document to true, but when the execution returns to the MainWindow constructor, ui->textEdit->document()->isModified() has the value 'false'. I am interested if anyone can reproduce this issue and if there is a solution to this problem.
-
I suspect the textEdit is empty before you call clear(), right? In that case I would consider that to be expected behaviour.
-
Yes but in on_textEditDocumentModificationChanged slot the line @qDebug() << " on_textEditDocumentModificationChanged:" <<
modified << ui->textEdit->document()->isModified(); // true true@ prints "true true". -
I would think that it does change the status while checking for changes (when clearing). I understand this might be strange at times, but to me it still looks like a valid behaviour ;)
What happens if you set some text in your textEdit before clearing?
-
I set some text in the textEdit before clearing and it's the same situation with the same output text.
-
OK, now it finally seems a bit weird to me :-) Sorry it took so long.
But, when you set the text, does it still show as isModified == false before the clearing?
Quoting from the documentation:
[quote]This property holds whether the document has been modified by the user.[/quote]I suspect that is the key here: it will return true when modified by the user, but not when being modified programatically. In that light, I think the fact that the state changes briefly when clear() is called is a bug, please report it.
-
In the end I added just below this line:
@ui->textEdit->clear();@
the following line:
@ui->textEdit->document()->setModified(true);@
This simulates the expected behavior.
I think that, when I have enough time for this, I will report this bug.
Thank you for your messages!