How to make an Application restartable: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Style)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:snippets]]
If you are in the need to make an application restartable depending on the user interaction you have to follow these steps:
 
'''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:
 
=== 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>
<code>
static int const EXIT_CODE_REBOOT;
<code>


and initialize it with a value:
and initialize it with a value:


</code>int const MainWindow::EXIT_CODE_REBOOT = -123456789;</code>
</code>
int const MainWindow::EXIT_CODE_REBOOT = -123456789;
</code>


Otherwise you can define a global variable or a constant value.
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 in your Application ==
Define a slot that will exits the application using the reboot code:
Next define a slot that will exit the application using the reboot code:


<code>void MainWindow::slotReboot()
<code>
void MainWindow::slotReboot()
{
{
  qDebug() << "Performing application reboot..";
  qDebug() << "Performing application reboot...";
  qApp->exit( MainWindow::EXIT_CODE_REBOOT );
  qApp->exit( MainWindow::EXIT_CODE_REBOOT );
}</code>
}
 
</code>
=== Create an action to handle the reboot ===


== Create an QAction 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:
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 );
<code>
actionReboot->setText( tr("Restart") );
actionReboot = new QAction( this );
actionReboot->setStatusTip( tr("Restarts the application") );
actionReboot->setText( tr("Restart") );
connect( actionReboot,
actionReboot->setStatusTip( tr("Restarts the application") );
SIGNAL (triggered()),
connect( actionReboot, SIGNAL (triggered()),
this,
                this, SLOT (slotReboot())
SLOT (slotReboot()) );</code>
      );
 
</code>
=== Modify the application cycle ===


The last step is to modify the application main function to handle the new cycle that will allow reboot:
== Modify the Application Cycle ==
The last step is to modify the application main function to handle the new cycle that will allow rebooting:


<code>int main(int argc, char *argv[])
<code>
int main(int argc, char *argv[])
{
{
  int currentExitCode = 0;
  int currentExitCode = 0;


do{
do {
QApplication a(argc, argv);
  QApplication a(argc, argv);
MainWindow w;
  MainWindow w;
w.show();
  w.show();
currentExitCode = a.exec();
  currentExitCode = a.exec();
 
} while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );
}while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );


return currentExitCode;
return currentExitCode;
}
</code>

Revision as of 19:58, 13 March 2015

If you are in 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

Next define a slot that will exit the application using the reboot code:

void MainWindow::slotReboot()
{
 qDebug() << "Performing application reboot...";
 qApp->exit( MainWindow::EXIT_CODE_REBOOT );
}

Create an QAction 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 rebooting:

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;
}