Results 1 to 6 of 6

Thread: How to close application?

  1. #1
    Join Date
    Aug 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default How to close application?

    Hi, I know this seems to be a stupid question, but I really have a problem. I've got this code:

    void Form1:penDialog(){
    Dialog *dialog=new Dialog(this);
    int result=dialog->exec();

    if(result==QDialog::Rejected){
    //here I want to close the application
    }

    delete dialog;
    }

    Form1 inherits from QMainWindow

    I've tried almost everything (qApp->quit(),close()) and nothing works. Maybe someone knows what to do.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to close application?

    Quote Originally Posted by beerkg
    I've tried almost everything (qApp->quit(),close()) and nothing works.
    qApp->quit() should work, unless you invoke openDialog() in a loop or you block the event loop in some way.

  3. #3
    Join Date
    Aug 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to close application?

    Sorry I dont quite understand: "unless you invoke openDialog() in a loop". You mean the dialog loop or main application loop? I want to quit after the dialog loop is finished. One thing i've noticed is when I use QMessageBox before qApp->quit() it worked. It looks like Dialog doesnt have enough time to finish its event loop before I use qApp->quit()

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to close application?

    Quote Originally Posted by beerkg
    You mean the dialog loop or main application loop?
    I meant something like:
    Qt Code:
    1. while( 1 ) {
    2. dlg.exec();
    3. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by beerkg
    I want to quit after the dialog loop is finished. One thing i've noticed is when I use QMessageBox before qApp->quit() it worked. It looks like Dialog doesnt have enough time to finish its event loop before I use qApp->quit()
    So qApp->quit() doesn't work or it works too fast?

  5. #5
    Join Date
    Aug 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to close application?

    When I do something like that:
    Qt Code:
    1. void Form1::openDialog(){
    2. Dialog *dialog=new Dialog(this);
    3. int result=dialog->exec();
    4.  
    5. if(result==QDialog::Rejected){
    6. QMessageBox::Information(this,"","");
    7. qApp->quit();
    8. }
    9.  
    10. delete dialog;
    11. }
    To copy to clipboard, switch view to plain text mode 

    it works fine, but without this meeage box it doesn't work.

  6. #6
    Join Date
    Aug 2006
    Location
    Zürich, Switzerland
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to close application?

    Hi

    From the Qt docs:

    Regarding qApp->quit(), qApp->exit() etc:
    Note that ... this function does return to the caller -- it is event processing that stops.


    Therefore try this: First terminate the Qt event loop processing and then end the application with the exit() function from stdlib.h:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <stdlib.h>
    3.  
    4. ...
    5. if(result == QDialog::Rejected)
    6. {
    7. QCoreApplication::quit(0);
    8. exit(0);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Regards
    Ernst

  7. The following user says thank you to 3dch for this useful post:

    aMan (11th August 2006)

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2006, 19:51
  2. How to install my own application in QUnitTest
    By Artschi in forum Qt Programming
    Replies: 3
    Last Post: 18th July 2006, 17:05
  3. Qmenu Stable close on QTableWidget
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2006, 11:03
  4. Replies: 5
    Last Post: 24th April 2006, 16:42
  5. Replies: 3
    Last Post: 31st March 2006, 19:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.