Results 1 to 5 of 5

Thread: QDialog not closing/going out of scope?

  1. #1

    Default QDialog not closing/going out of scope?

    I don't know why a.exec() won't return and end the process! It's a simple code:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. w.setAttribute(Qt::WA_QuitOnClose);
    5. w.exec();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    It was supposed to open a dialog and when I hit the 'X' button it should close and end the process, but instead the process keeps running!.. Anyone knows why?
    P.S: I know I could've used QDIalog::show() method instead of QDIalog::exec() and it would work fine, but I really want to understand why it won't work this way. This is just a simple code, but I'll need it to construct more complex ones.

    Thanks o/

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDialog not closing/going out of scope?

    It's because you create two separate event-loops:
    First here
    Qt Code:
    1. w.exec(); //This can't close the application since application event loop didn't start.
    To copy to clipboard, switch view to plain text mode 
    and the second one here:
    Qt Code:
    1. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    And this loop is practically waiting for some window to show and after that window (that never show - because your dialog is already closed when execution reaches this second event-loop) is closed the application finishes.

    So you don't need to use w.exec() there, just use show, you can use exec() if you create a second dialog and you want that dialog to be modal.

  3. #3

    Default Re: QDialog not closing/going out of scope?

    I see!! Thanks

  4. #4
    Join Date
    Mar 2006
    Posts
    74
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog not closing/going out of scope?

    snip
    So you don't need to use w.exec() there, just use show, you can use exec() if you create a second dialog and you want that dialog to be modal.
    I think this should be "... use exec() if you create a second dialog and you don't want that dialog to be modal."

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDialog not closing/going out of scope?

    Quote Originally Posted by hvengel View Post
    I think this should be "... use exec() if you create a second dialog and you don't want that dialog to be modal."
    No, calling exec() will create a modal dialog.
    Or to rephrase it: you call exec() when you need to create a second dialog and you want that second dialog to be modal.

Similar Threads

  1. Replies: 9
    Last Post: 25th March 2011, 21:22
  2. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  3. QDialog not closing properly
    By goocreations in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2010, 06:47
  4. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  5. Closing a QDialog
    By eu.x in forum Newbie
    Replies: 2
    Last Post: 12th March 2007, 08:33

Tags for this Thread

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.