First, Introduction
Recently due to project requirements, the QT program once detected an error, to restart, each time you close the main window of all the child windows but some modal box problems, so from the online summary of some knowledge points for future applications.
Ii. detailed 1, QT structure
[CPP]View Plaincopy
- int main (int argc, char *argv[])
- {
- Qapplication A (argc, argv);
- Mywidget W;
- Mydialog Dialog; //New Mydialog class object
- if (dialog.exec () ==qdialog::accepted) { //Judgment Dialog Execution result
- W.show (); //If the "Go to Main Interface" button is pressed, the main interface is displayed
- return a.exec (); //Program normal operation
- }
- else return 0; //Otherwise, exit the program
- }
Execute to A.exec () to start the event loop of the main thread, exit the loop both to exit the event loop and return the value of a.exec () or 0.
(2) Exit of main interface
Exit mode in the lower right corner of the main window: qcoreapplication::exit (0); 0 is the value that the state can also be.
Mode two: Qcoreapplication::quit ();
Way three: Qapplication::exit (0); Qapplication is inherited from Qcoreapplication.
Mode four: Qapplication::quit ();
Mode five: Close (); Qapplicatio has a common attribute of qapp->quitonlastwindowclosed (true) and automatically calls the previous exit () when the last window is closed.
Mode six: Qapplication::closeallwindows (); Closing multiple windows is better than calling quit because the window can accept the close event.
Of course, you can also loop off all qwidget windows:
[CPP]View Plaincopy
- Qobjectlist list = Mainwindow->children ();
- foreach (Qobject *obj, list) {
- if (qobject_cast<qwidget *> (obj)) {
- Qobjectlist List_son = qobject_cast<qwidget *> (obj)->children ();
- foreach (Qobject *obj_son, List_son) {
- if (qobject_cast<qwidget *> (Obj_son)) {
- Qobject_cast<qwidget *> (Obj_son)->close ();
- }
- }
- Qobject_cast<qwidget *> (obj)->close ();
- }
- }
To close all Qdialog windows only:
[HTML]View Plaincopy
- Qobjectlist list = this->children ();
- foreach (Qobject *obj, list) {
- if (Qobject_cast<qdialog *> (obj)) {
- Qobjectlist List_son = qobject_cast<qdialog *> (obj)->children ();
- foreach (Qobject *obj_son, List_son) {
- if (Qobject_cast<qdialog *> (Obj_son)) {
- Qobject_cast<qdialog *> (obj_son)->close ();
- }
- }
- Qobject_cast<qdialog *> (obj)->close ();
- }
- }
(3) One of the restart programs
Using Qprocess to start another process for the current program, the two programs do not have a parent-child relationship.
Click the Restart button and the contents of the slot
[CPP]View Plaincopy
- void Mywidget::slotbutton ()
- {
- Qapp->closeallwindows ();
- Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
- }
You can also use Qprocess::startdetached (Qapp->applicationfilepath ()), but make sure that no spaces are included in the path and there are no parameters.
Qapp->quit (); and qapp->closeallwindows (); All can be used, just to see if the Close event is accepted.
(4) Restart Program II
Write in the slots above:
[CPP]View Plaincopy
- void Mywidget::slotbutton ()
- {
- Qapp->closeallwindows ();
- Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
- Qapp->exit (0);
- }
In the main function
[CPP]View Plaincopy
- int main (int argc, char *argv[])
- {
- Qapplication A (argc, argv);
- int ret;
- Mywidget W;
- Mydialog Dialog;
- //New Mydialog class object
- if (dialog.exec () ==qdialog::accepted) { //Judgment Dialog Execution result
- W.show (); //If the "Go to Main Interface" button is pressed, the main interface is displayed
- ret = A.exec (); //Program normal operation
- }
- else return 0; //Otherwise, exit the program
- if (ret = = 0) {
- Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
- return 0;
- }
- return ret;
- }
Note that the return value of exit is the same as the value of RET to restart.
Iii. Summary
(1) I like to use one of the restart program, the location is simple, in the need to restart the place directly into the line.
(2) This test code is relatively simple, temporarily do not upload, you need to contact.
(3) My train of thought is limited, if has the better design suggestion, also may send the mail communication, in this first thanks! email address [email protected]
http://blog.csdn.net/taiyang1987912/article/details/39058775
About QT 20 app automatically restarts and closes Subwindow (six methods)