About QT 20 app automatically restarts and closes child windows

Source: Internet
Author: User

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 
  1. int main (int argc, char *argv[])
  2. {
  3. Qapplication A (argc, argv);
  4. Mywidget W;
  5. Mydialog Dialog; //New Mydialog class object
  6. if (dialog.exec () ==qdialog::accepted) { //Judgment Dialog Execution result
  7. W.show (); //If the "Go to Main Interface" button is pressed, the main interface is displayed
  8. return a.exec (); //Program normal operation
  9. }
  10. else return 0; //Otherwise, exit the program
  11. }

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 
  1. Qobjectlist list = Mainwindow->children ();
  2. foreach (Qobject *obj, list) {
  3. if (qobject_cast<qwidget *> (obj)) {
  4. Qobjectlist List_son = qobject_cast<qwidget *> (obj)->children ();
  5. foreach (Qobject *obj_son, List_son) {
  6. if (qobject_cast<qwidget *> (Obj_son)) {
  7. Qobject_cast<qwidget *> (Obj_son)->close ();
  8. }
  9. }
  10. Qobject_cast<qwidget *> (obj)->close ();
  11. }
  12. }

To close all Qdialog windows only:

[HTML]View PlainCopy
  1. Qobjectlist list = this->children ();
  2. foreach (Qobject *obj, list) {
  3. if (Qobject_cast<qdialog *> (obj)) {
  4. Qobjectlist List_son = qobject_cast<qdialog *> (obj)->children ();
  5. foreach (Qobject *obj_son, List_son) {
  6. if (Qobject_cast<qdialog *> (Obj_son)) {
  7. Qobject_cast<qdialog *> (obj_son)->close ();
  8. }
  9. }
  10. Qobject_cast<qdialog *> (obj)->close ();
  11. }
  12. }

(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  
    1. void Mywidget::slotbutton ()
    2. {
    3. Qapp->closeallwindows ();
    4. Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
    5. }

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 
    1. void Mywidget::slotbutton ()
    2. {
    3. Qapp->closeallwindows ();
    4. Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
    5. Qapp->exit (0);
    6. }

In the main function

[CPP]View PlainCopy 
  1. int main (int argc, char *argv[])
  2. {
  3. Qapplication A (argc, argv);
  4. int ret;
  5. Mywidget W;
  6. Mydialog Dialog;
  7. //New Mydialog class object
  8. if (dialog.exec () ==qdialog::accepted) { //Judgment Dialog Execution result
  9. W.show (); //If the "Go to Main Interface" button is pressed, the main interface is displayed
  10. ret = A.exec (); //Program normal operation
  11. }
  12. else return 0; //Otherwise, exit the program
  13. if (ret = = 0) {
  14. Qprocess::startdetached (Qapp->applicationfilepath (), qstringlist ());
  15. return 0;
  16. }
  17. return ret;
  18. }

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 child windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.