Results 1 to 5 of 5

Thread: QDialog - show(), exec() and blocking

  1. #1
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default QDialog - show(), exec() and blocking

    Hi,

    I just came across a widget with the following code in one of its methods (slightly paraphrased):
    Qt Code:
    1. void MyWidget::showDialog()
    2. {
    3. Ui_Dialog dui;
    4. QDialog dialog;
    5. dui.setupUi(&dialog);
    6. // some more stuff to setup dialog
    7. dialog.show();
    8. if (dialog.exec() == QDialog::Accepted)
    9. {
    10. // store dialog content somewhere
    11. }
    12. qDebug() << "END";
    13. }
    To copy to clipboard, switch view to plain text mode 

    I'm rather surprised by the show() followed by an exec() which I've never seen before in this way.
    What seems to happen is that the new dialog does not block the parent widget, because of the show().
    I don't understand why a previous show() should have an impact on the behaviour of exec().
    MyWidget remains responsive yet the rest of showDialog() does not get executed (no "END" is printed) until the dialog is closed (then it does get printed).
    So basically the method seems to pause at the exec() yet MyWidget is responsive?!
    Of course this leads to segmentation faults, if MyWidget gets closed before the dialog is closed.

    If I comment out the show(), the exec() properly blocks and everything works pretty much as I would expect.

    Is there any reason to use show() and exec() in this way (in any situation)?
    Is it a wanted behaviour that a previous show() influences exec() in this way?
    Isn't the correct way to open a dialog and remain responsive to connect a slot to the dialog's finished() method (and have the dialog be a member instead of a local variable)?

    Best,
    xdn
    Last edited by xdn; 9th April 2015 at 14:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog - show(), exec() and blocking

    show() is totally unnecessary here, exec() calls show() internally.

    exec() runs a nested event loop which processes all applications events just like the main event loop but filters out all user events targetted at the dialog's parent.
    A so called modal dialog.

    Caling just show() creates a non-modal dialog, which, as you correctly assumed, would need to be stored in a member variable instead of on the slot's stack.

    But either way is correct, it depends on the use case of the dialog.

    Not that the code you have there isn't horrible in other ways, the stray show() is the smallest problem

    Cheers,
    _

  3. #3
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: QDialog - show(), exec() and blocking

    Quote Originally Posted by anda_skoa View Post
    Not that the code you have there isn't horrible in other ways, the stray show() is the smallest problem
    Hi again,

    thanks for clearing that up for me.
    I am interested in what else you think is horrible in that code snippet.

    Best,
    xdn

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog - show(), exec() and blocking

    This part
    Qt Code:
    1. Ui_Dialog dui;
    2. QDialog dialog;
    3. dui.setupUi(&dialog);
    To copy to clipboard, switch view to plain text mode 

    The proper way to have a custom dialog is to derive from QDialog and do the UI setup in its constructor.
    Which is, not incidentally, also the way QtCreator's "New UI Form class" template works.

    The code using the dialog should not have to care about setting up dialog internals.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: QDialog - show(), exec() and blocking

    Ok, will fix that part as well.
    Thanks for all the help.

Similar Threads

  1. QSqlQuery exec blocking function
    By reinki0013 in forum Qt Programming
    Replies: 5
    Last Post: 13th August 2012, 18:00
  2. Replies: 9
    Last Post: 25th March 2011, 21:22
  3. exec() not blocking, derived QDialog, Qt 4.4.3
    By wdezell in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2009, 18:56
  4. problem with show/exec of Qdialog
    By dudedude in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 11:20
  5. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 11:43

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.