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

From Qt Wiki
Revision as of 11:25, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

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.

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

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&quot;);
settings.setValue("quitWithoutPrompt&quot;, false);
settings.endGroup();
}