How to make an Application restartable: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(syntaxhighlight)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
[[Category:HowTo]]
 
{{LangSwitch}}
'''English''' | [[ApplicationRestart_German|Deutsch]]
If you are in the need to make an application restartable depending on the user interaction you have to follow these steps:
 
= 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 = <s>123456789;</code>
<code>
<br />Otherwise you can define a global variable or a constant value.
int const MainWindow::EXIT_CODE_REBOOT = -123456789;
<br />h3. Define a slot in your application main window that will perform the exit from the application
</code>
<br />Define a slot that will exits the application using the reboot code:
<br /><code>void MainWindow::slotReboot()<br />{<br /> qDebug() &lt;&lt; &quot;Performing application reboot..&quot;;<br /> qApp</s>&gt;exit( MainWindow::EXIT_CODE_REBOOT );<br />}</code>


=== Create an action to handle the reboot ===
Otherwise you can define a global variable or a constant value.


Create an action that will consume the above slot in order to exit with the reboot code. Something like the following will work:
== Define a Slot in your Application ==
Next define a slot that will exit the application using the reboot code:


<code> actionReboot = new QAction( this );<br /> actionReboot-&gt;setText( tr(&quot;Restart&amp;quot;) );<br /> actionReboot-&gt;setStatusTip( tr(&quot;Restarts the application&amp;quot;) );<br /> connect( actionReboot,<br /> SIGNAL (triggered()),<br /> this,<br /> SLOT (slotReboot()) );</code>
<code>
void MainWindow::slotReboot()
{
qDebug() << "Performing application reboot...";
qApp->exit( MainWindow::EXIT_CODE_REBOOT );
}
</code>


=== Modify the application cycle ===
== 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:


The last step is to modify the application main function to handle the new cycle that will allow reboot:
<code>
actionReboot = new QAction( this );
actionReboot->setText( tr("Restart") );
actionReboot->setStatusTip( tr("Restarts the application") );
connect( actionReboot, SIGNAL (triggered()),
                this, SLOT (slotReboot())
      );
</code>


<code>int main(int argc, char *argv[])<br />{<br /> int currentExitCode = 0;
== Modify the Application Cycle ==
The last step is to modify the application main function to handle the new cycle that will allow rebooting:


do{<br /> QApplication a(argc, argv);<br /> MainWindow w;<br /> w.show();<br /> currentExitCode = a.exec&amp;amp;#40;&amp;#41;;
<code>
int main(int argc, char *argv[])
{
int currentExitCode = 0;


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


return currentExitCode;
return currentExitCode;
}
</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;
}