Introduction
Today's shared content has some meaning-how to restart an application. In fact, sometimes this is a very important function point, and very user-friendly, easy to use very good.
For example: Switch users. When a user logs in successfully and needs to switch to another account, then you know how important it is.
Introduction Effect Qapp Exit code Restart define exit code Restart action Modify application loop qprocess Restart source download
effect
Qapp exit code Restart Defining exit Codes
In a better way, define a static variable in the main window:
static int const Exit_code_reboot;
and initialize:
int const WIDGET::EXIT_CODE_REBOOT =-123456789;
Or you can define a global variable or a constant value. Restart Operation
Next, define a slot function that contains the code for the application restart:
void Widget::reboot ()
{
qapp->exit (widget::exit_code_reboot);
}
Create an action that will use the slots above to restart the program.
Qpushbutton *pbutton = new Qpushbutton (this);
Pbutton->settext (qstringliteral ("restart"));
Connect (Pbutton, SIGNAL (clicked (bool)), this, SLOT (reboot ()));
Modifying Application Loops
The final step, modify the application's main function to handle the new loop, will allow the program to restart:
int main (int argc, char *argv[])
{
int nexitcode = 0;
Do {
qapplication a (argc, argv);
Widget W;
W.show ();
Nexitcode = A.exec ();
} while (Nexitcode = = widget::exit_code_reboot);
return nexitcode;
}
The above approach is done, and there is an easier way to start with qprocess. qprocess Restart
There is no need to define a restart code, and there is no need to modify the application loop. Just a simple slot function is required.
Define a slot function that contains the code for the application restart:
void Widget::reboot ()
{
QString program = Qapplication::applicationfilepath ();
Qstringlist arguments = qapplication::arguments ();
QString workingdirectory = Qdir::currentpath ();
Qprocess::startdetached (program, arguments, workingdirectory);
Qapplication::exit ();
}
Source DownloadQT Restart App