In my codes, I use a flag changed to mark whether the document has been modified:
MainWindow::MainWindow(QWidget *parent)
{
changed = false;
……
In fact, Qt provides a special method of QTextDocument(which is the type textEdit->document() points to) :
bool QTextDocument::isModified()
which indicates whether the document has been modified.
And the maybeSave() function is another common skill :
bool MainWindow::maybeSave()
{
if (textEdit->document()->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return save();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}
To be specific, it shows the usage of a class: QMessageBox.
Or, more specificly, a basic usage.
In this scrap, an enum type QMessageBox::StandardButton and a static method QMessageBox::warning.
StandardButton includes 19 kinds of common button like "Yes","No","Save","Discard"etc. and warning shows a dialog and return
the StandardButton the user choose.