Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to make an Application restartable: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (code -> pre)
(syntaxhighlight)
 
Line 6: Line 6:
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:


<pre>
<code>
static int const EXIT_CODE_REBOOT;
static int const EXIT_CODE_REBOOT;
</pre>
</code>


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


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


Otherwise you can define a global variable or a constant value.
Otherwise you can define a global variable or a constant value.
Line 21: Line 21:
Next define a slot that will exit the application using the reboot code:
Next define a slot that will exit the application using the reboot code:


<pre>
<code>
void MainWindow::slotReboot()
void MainWindow::slotReboot()
{
{
Line 27: Line 27:
  qApp->exit( MainWindow::EXIT_CODE_REBOOT );
  qApp->exit( MainWindow::EXIT_CODE_REBOOT );
}
}
</pre>
</code>


== Create a QAction to handle the Reboot ==
== Create a 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:


<pre>
<code>
actionReboot = new QAction( this );
actionReboot = new QAction( this );
actionReboot->setText( tr("Restart") );
actionReboot->setText( tr("Restart") );
Line 39: Line 39:
                 this, SLOT (slotReboot())
                 this, SLOT (slotReboot())
       );
       );
</pre>
</code>


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


<pre>
<code>
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
Line 58: Line 58:
  return currentExitCode;
  return currentExitCode;
}
}
</pre>
</code>

Latest revision as of 02:51, 18 May 2017

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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;

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