How to make an Application restartable

From Qt Wiki
Revision as of 19:50, 13 March 2015 by Simow (talk | contribs) (Simow moved page ApplicationRestart to How to make an Application restartable: Wording)
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.

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.

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()
{
 qDebug() << "Performing application reboot..";
 qApp->exit( MainWindow::EXIT_CODE_REBOOT );
}

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 );
 actionReboot->setText( tr("Restart") );
 actionReboot->setStatusTip( tr("Restarts the application") );
 connect( actionReboot,
 SIGNAL (triggered()),
 this,
 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();

}while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );

return currentExitCode;