Diaries of Learning Qt (1.3) The maybeSave() Function

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.

 

posted @ 2017-08-27 18:46  Dolviet  阅读(217)  评论(0编辑  收藏  举报