How to make an Application restartable: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=Making an application restartable/rebootable= | [[Category:HowTo]]<br />[[Category:snippets]] | ||
'''English''' | [[ApplicationRestart_German|Deutsch]] | |||
= Making an application restartable/rebootable = | |||
If you have the need to make an application restartable depending on the user interaction you have to follow these steps: | If you have the need to make an application restartable depending on the user interaction you have to follow these steps: | ||
===Create an exit code that represents your reboot/restart event=== | === Create an exit code that represents your reboot/restart event === | ||
It is a good idea to define this code as a static variable in your main window: | It is a good idea to define this code as a static variable in your main window: | ||
<code>static int const EXIT_CODE_REBOOT;<code> | |||
and initialize it with a value: | and initialize it with a value: | ||
Otherwise you can define a global variable or a constant value. | </code>int const MainWindow::EXIT_CODE_REBOOT = <s>123456789;</code> | ||
<br />Otherwise you can define a global variable or a constant value. | |||
<br />h3. Define a slot in your application main window that will perform the exit from the application | |||
<br />Define a slot that will exits the application using the reboot code: | |||
<br /><code>void MainWindow::slotReboot()<br />{<br /> qDebug() << "Performing application reboot..";<br /> qApp</s>>exit( MainWindow::EXIT_CODE_REBOOT );<br />}</code> | |||
=== Create an action to handle the reboot === | |||
Create an action that will consume the above slot in order to exit with the reboot code. Something like the following will work: | |||
<code> actionReboot = new QAction( this );<br /> actionReboot->setText( tr("Restart&quot;) );<br /> actionReboot->setStatusTip( tr("Restarts the application&quot;) );<br /> connect( actionReboot,<br /> SIGNAL (triggered()),<br /> this,<br /> SLOT (slotReboot()) );</code> | |||
=== | === Modify the application cycle === | ||
The last step is to modify the application main function to handle the new cycle that will allow reboot: | |||
= | <code>int main(int argc, char *argv[])<br />{<br /> int currentExitCode = 0; | ||
do{<br /> QApplication a(argc, argv);<br /> MainWindow w;<br /> w.show();<br /> currentExitCode = a.exec&amp;#40;&#41;; | |||
== | }while( currentExitCode == MainWindow::EXIT_CODE_REBOOT ); | ||
return currentExitCode; | |||
Revision as of 08:59, 24 February 2015
English | Deutsch
Making an application restartable/rebootable
If you have the need to make an application restartable depending on the user interaction you have to follow these steps:
Create an exit code that represents your reboot/restart event
It is a good idea to define this code as a static variable in your main window:
static int const EXIT_CODE_REBOOT;<code>
and initialize it with a value:
int const MainWindow::EXIT_CODE_REBOOT = 123456789;
Otherwise you can define a global variable or a constant value.
h3. Define a slot in your application main window that will perform the exit from the application
Define a slot that will exits the application using the reboot code:
void MainWindow::slotReboot()<br />{<br /> qDebug() << "Performing application reboot..";<br /> qApp</s>>exit( MainWindow::EXIT_CODE_REBOOT );<br />}
Create an action to handle the reboot
Create an action that will consume the above slot in order to exit with the reboot code. Something like the following will work:
actionReboot = new QAction( this );<br /> actionReboot->setText( tr("Restart&quot;) );<br /> actionReboot->setStatusTip( tr("Restarts the application&quot;) );<br /> connect( actionReboot,<br /> SIGNAL (triggered()),<br /> this,<br /> SLOT (slotReboot()) );
Modify the application cycle
The last step is to modify the application main function to handle the new cycle that will allow reboot:
int main(int argc, char *argv[])
{
int currentExitCode = 0;
do{
QApplication a(argc, argv);
MainWindow w;
w.show();
currentExitCode = a.exec&#40;);
}while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );
return currentExitCode;