Results 1 to 9 of 9

Thread: QDialog: show() and exec() together in constructor?

  1. #1
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDialog: show() and exec() together in constructor?

    I'm using a QDialog. In the constructor I need to do some time-consuming stuff. Therfore it looks like this:

    constructor
    {
    initialize widgets, buttons, etc.
    mydialog->show();
    do some timeconsuming stuff
    mydialog->exec();
    }

    My question is, is it valid/legal to use show() and exec() in the same constructor?
    I tried and everything works well but I want to know for sure.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QDialog: show() and exec() together in constructor?

    Em..interesting question Yes its correct, but why do you need it ?
    exec() - show your dialog as modal window;
    show() - show your dialog as standart widget;
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDialog: show() and exec() together in constructor?

    Hmm, I doubt it does what it's intended to do. Calling show() doesn't show the dialog immediately but schedules a show event which the dialog will receive later when the control returns to the event loop. Calling exec() makes the dialog appear because the dialog will start it's own event loop.
    J-P Nurmi

  4. #4
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog: show() and exec() together in constructor?

    exec() blocks till the dialog gets closed, show() doesn't.

    If I do like this:

    Qt Code:
    1. constructor
    2. {
    3. initialize widgets, buttons, etc.
    4. do some timeconsuming stuff
    5. mydialog->exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    it will take a long time till the user sees the dialog because of the time consuming stuff in the constructor.

    If I do like this:

    Qt Code:
    1. constructor
    2. {
    3. initialize widgets, buttons, etc.
    4. mydialog->exec();
    5. do some timeconsuming stuff
    6. }
    To copy to clipboard, switch view to plain text mode 

    then the timeconsuming stuff will be done AFTER the user closed the dialog

    If I do like this:

    Qt Code:
    1. constructor
    2. {
    3. initialize widgets, buttons, etc.
    4. mydialog->show();
    5. do some timeconsuming stuff
    6. }
    To copy to clipboard, switch view to plain text mode 

    then the parent window/widget will continue (the dialog will not block) and that's not what I want.

    Regards.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDialog: show() and exec() together in constructor?

    Quote Originally Posted by Teuniz View Post
    exec() blocks till the dialog gets closed, show() doesn't.
    Yes, that's the difference between modal and modeless dialogs. But calling show() before the time consuming task doesn't bring the dialog visible until exec() gets called, unless you call QCoreApplication::processEvents() during the time consuming task. There is a little pitfall in this, though, because the dialog would end up being modeless during the time consuming task and turn to a modal dialog after the task is finished and exec() is called. This can be avoided by calling QDialog::setModal(true) before the time consuming task.

    Qt Code:
    1. MyDialog d; // it's safe to allocate the dialog on the stack, because exec() blocks
    2. mydialog.setModal(true);
    3. mydialog.show();
    4. for (...)
    5. {
    6. // do one part of the time consuming task
    7. qApp->processEvents(); // let the app process it's events every once in a while
    8. }
    9. mydialog.exec(); // let the dialog start it's own event loop
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDialog: show() and exec() together in constructor?

    How about:
    Qt Code:
    1. MyDialog d;
    2. QTimer::singleShot(0, &d, SLOT(doTimeConsumingTask()));
    3. d.exec();
    To copy to clipboard, switch view to plain text mode 

    This should execute the slot immediately when d.exec() is called (provided that the event will not make it to a wrong queue). The drawback is that you will block the event loop if you do everything in the slot at once. So either do it the way J-P mentioned or split the task into subtasks, put each in a separate slot and as the last line of each slot start a timer that will trigger the next slot.

  7. #7
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog: show() and exec() together in constructor?

    Yes, I discovered this by trial and error Thanks anyway for your good explanation.

    By the way, instead of using
    Qt Code:
    1. qApp->processEvents();
    To copy to clipboard, switch view to plain text mode 
    I use
    Qt Code:
    1. for(i=0; i<10; i++) qApp->processEvents();
    To copy to clipboard, switch view to plain text mode 
    because I have the impression that (specially under Windows) calling processEvents() only
    one time isn't always enough when the OS is very busy (widgets aren't painted completely etc.).

    Regards.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDialog: show() and exec() together in constructor?

    processEvents() processes all events pending. The problem widgets are not painted is because you made the processor very busy. Instead use slots that'll get triggered every 100ms or so - the task will do its job slower, but the system stability will be sustained.

  9. #9
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog: show() and exec() together in constructor?

    I will use the solution with the QTimer, it gives better results than using qApp->processEvents();

    Thanks for your help.

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.