Quit-application-action-with-do-no-show-again-option

From Qt Wiki
Revision as of 16:55, 3 March 2015 by AutoSpider (talk | contribs) (Add "cleanup" tag)
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

Usually applications have a Quit action in the context menu, The following code will show a question dialog to the user to make sure that (s)he really wants to quit. There is also an option to supress this message.

void MainWindow::on_actionQuit_triggered()
{
 bool exit = false;
 QSettings settings;
 settings.beginGroup("MainWindow");
 if (settings.value("quitWithoutPrompt").toBool()) {
 exit = true;
 } else {
 QMessageBox quitQuestion;
 QCheckBox checkBox;
 checkBox.setText(tr("Do not ask again"));
 quitQuestion.setCheckBox(&checkBox);
 quitQuestion.setText(tr("Do you really want to exit this program?"));
 quitQuestion.setWindowTitle(tr("Are you sure?"));
 quitQuestion.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
 quitQuestion.setDefaultButton(QMessageBox::No);
 quitQuestion.exec();
 if (quitQuestion.result() == QMessageBox::Yes) {
 settings.setValue("quitWithoutPrompt", checkBox.isChecked());
 exit = true;
 }
 }
 settings.endGroup();
 if (exit)
 QCoreApplication::quit();
}

To reenable this message you will need to clear the mainwindow/quitWithoutPrompt key trough the default QSettings. The following code can be used do do that:

void MainWindow::setQuitQuestinEnabled() {

QSettings settings;
settings.beginGroup("MainWindow");
settings.setValue("quitWithoutPrompt", false);
settings.endGroup();

}