I have written a text editing program similar to Notepad in C++ with the Qt framework. It only has basic functionality such as Undo, Redo etc. I want to show a dialog which will show up if the user hasn't saved his changes and ask whether he/she wants to save the changes similar to the one in Notepad and other such programs. How should I proceed with this?
            Asked
            
        
        
            Active
            
        
            Viewed 1,996 times
        
    2 Answers
3
            Qt's documentation for QMessageBox discusses this and provides the following example: -
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
  case QMessageBox::Save:
      // Save was clicked
      break;
  case QMessageBox::Discard:
      // Don't Save was clicked
      break;
  case QMessageBox::Cancel:
      // Cancel was clicked
      break;
  default:
      // should never be reached
      break;
}
Applications can monitor when they're about to quit via the aboutToQuit signal, which is where you would prompt the user.
For example (with C++ 11)
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    MainWindow w;
    // Note Qt 5 connect syntax with C++ 11 lambda function
    QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&w](){
        qDebug() << "Terminating - Goodbye!\n";
        //Display msg prompt here
        if(QMessageBox::question(NULL, "Test", "Save?", QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
        {
            // if result is save...
            w.save();
        }
    });
    w.show();
    return a.exec();
}
 
    
    
        Community
        
- 1
- 1
 
    
    
        TheDarkKnight
        
- 27,181
- 6
- 55
- 85
- 
                    How will I check whether the document has been saved first. Only if the document isn't saved, this dialog should popup. – Vishal Subramanyam Nov 24 '15 at 12:05
- 
                    If you're using `QTextDocument`, you can use its [changed](http://doc.qt.io/qt-5/qtextdocument.html#contentsChanged) signal, else you'll have to monitor that yourself. Then, maintain a boolean flag, which you set to false when the document is loaded. With any change made, set it to true, then after saving, set it back to false. – TheDarkKnight Nov 24 '15 at 13:04
- 
                    How do I implement the Cancel function if I am to use your example? – Vishal Subramanyam Nov 24 '15 at 15:42
- 
                    1If you want to cancel the closing of the application, you can check for saving in the [closeEvent](http://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button) of the MainWindow, instead of the main function. – TheDarkKnight Nov 24 '15 at 16:13
- 
                    Thanks @TheDarkKnight for your advice. When I finish my program, I will try to send a copy of it to you. – Vishal Subramanyam Nov 25 '15 at 06:01
- 
                    2`aboutToQuit` cannot be used here, see the docs: "The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state." closeEvent needs to be overridden to be able to say "no, don't quit". – exscape Jun 05 '16 at 10:25
- 
                    @exscape, thanks for pointing out the documentation, but a QMessageBox does work in this instance. Try it for yourself, before marking down the answer. – TheDarkKnight Jun 06 '16 at 08:11
- 
                    It wouldn't work if you want to have "cancel" as an option, and applications that don't are pretty user-unfriendly. – exscape Jun 06 '16 at 08:56
- 
                    @exscape, the question being asked isn't requesting whether or not cancelling the quit operation is an option. It asks how to prompt the user to save changes before the quit occurs. – TheDarkKnight Jun 06 '16 at 13:01
- 
                    I'm aware, but such a dialog should IMO always have the cancel option. Every well-designed application allows you to cancel the shutdown. – exscape Jun 06 '16 at 18:22
1
            
            
        you can do something like this:
        Document unsaveDocument = getUnsaveDocument();
        if (unsaveDocument.isModified()) {
           QMessageBox *alert = new QMessageBox;
           alert->setWindowTitle("File is modified");
           alert->setText("Do you want to save your changes?");
           alert->setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
           alert->setDefaultButton(QMessageBox::Save);
           int ret = alert->exec();
           alert->deleteLater();
           if (ret == QMessageBox::Save) {
               saveDocument();
           } else {
               doSomeThing(); //When discard or cancel
           }
       }
 
    
    
        tanhieu
        
- 86
- 4
