Sample Programs
Code upload to Https://github.com/gatieme/AderXCoding/tree/master/qt/restart problem description
Sometimes the written program sometimes runs into problems, expecting to be resolved by restarting, but there is no interface in our QT that implements such functionality, and this has to be done by ourselves.
Reboot is simply to close the current application and then start a new process to execute the current application How to implement a reboot
Currently there are two ways to implement a reboot
Method |
Description |
Process Control |
Exit the current process and start a new process via qprocess |
Event Loops |
Exit the application, and then start by using the Application event loop program |
using Process Control to implement a reboot
First look at how QT program exits QT program Exit
The general structure of the QT program is as follows:
int main (int argc, char** argv)
{
qapplication app (argc, argv);
Widget W;
W.show () return
app.exec ();
}
The last sentence starts the event loop for the main thread. To exit the program, you exit the event loop and return the main function.
So exiting the application requires exiting the application Qapplication exit && quit
There are two simple ways to exit.
Generally speaking, the correct exit method is as follows
Qcoreapplication::exit (int);
There's another vest.
Qcoreapplication::quit (); <==> exit (0);
We can easily find the difference and connection between quit and exit in the official help document.
closeallwindows
For Qapplication, it has a common property quitonlastwindowclosed default is True, so when the last window closes, it can automatically call the front exit ()
So closing the program we just need to close all the programs and guarantee quitonlastwindowclosed = True.
So we can also use
Qapplication::closeallwindows ()
Note: Generally speaking, for multiple-window programs, calling this would be better than calling quit directly. Because such a window can receive a close event. Restart a process
In Qt, you can use qprocess::startdetached to open a new process
To start another process for the current program, you can use qprocess if you want to make the two without a "parent-child" relationship
The role of the Qprocess class is to initiate and interact with an external program.
Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
But why not write the following.
Qprocess::startdetached (Qapp->applicationfilepath ());
Of course, this can work if the path does not contain spaces. implementation restart through Process Control
Use Process Control to implement reboot///do the following///① exit the current program (6 ways to implement in QT)///② open a new process start the current program (using qprocess::startdetached)//////① Way to implement exit
Mode one: Qcoreapplication::exit (0); 0 is the state or other value.
Qcoreapplication::quit ();
Qapplication::exit (0);
Qapplication is inherited from Qcoreapplication.
Qapplication::quit (); Mode two: Close ();
Qapplicatio has a common property qapp->quitonlastwindowclosed (True), and the preceding exit () is automatically invoked when the last window closes.
Qapplication::closeallwindows (); Closing multiple windows is better than calling quit because the window can accept the close event. ② opens a new process to start the current program/////In QT, you can use qprocess::startdetached to open a new process//To start another process of the current program, have to make the two have no "parent-child" relationship/use Qprocess::
Startdetached (Qapp->applicationfilepath (), qstringlist ());
where Qstringlist () is the parameter list//special, The Qprocess::startdetached (Qapp->applicationfilepath ()) can be used if there are no spaces in the path of the program.
void Restartwidget::slotproceerestart () {qapp->closeallwindows ();
Qapp->quit ();
If it is on the embedded arm board, it is necessary to add the-qws parameter//qstringlist args; Args.append ("-qws"); Parameter Information//arGs.append ("&");
Background Run qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ()); }
using Event Loops
Observing the previous QT main function, we see the last line of A.exec ();
This function causes the QT GUI to enter the event loop and returns the value of the program exit.
So we can do something in this place.
The easiest way to do this is to create a loop that, when the function exits normally, does not process, and the program expects the program to return a exit_code that we can identify and not actually quit, but to restart a new form into the event loop
It's no good, but sometimes I seem to prefer to write this:
Use event loops to restart
void Restartwidget::soltexitrestart ()
{
// 773 = ' R ' + ' E ' + ' + ' + ' t ' + ' a ' + ' r ' + ' t '
// ==>restart
//#define Exit_restart 773
qapp->exit (773);
}
Then the main function is changed to
int main (int argc, char *argv[])
{
qapplication A (argc, argv);
int ret =-1;
Do
{
restartwidget w;
W.show ();
ret = A.exec ();
} while (ret!= 0);
return ret;
}