Results 1 to 5 of 5

Thread: Qt::WA_DeleteOnClose while a child QDialog is executed

  1. #1
    Join Date
    Nov 2007
    Posts
    53
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt::WA_DeleteOnClose while a child QDialog is executed

    Hi,

    I have a QMainWindow subclass with the attribute Qt::WA_DeleteOnClose set.

    In some cases, I open a QDialog (and more exactly QWizard) that is a child of QMainWindow.

    If the user interact with a particular QAction from the systray icon, I close the QMainWindow subclass programatically. The problem is after the call to the QMainWindow destructor, the control flow returned right after the QWizard exec call :

    Qt Code:
    1. if(wizard)
    2. {
    3. if(wizard->exec())
    4. {
    5. if(wizard->field("displayListing").toBool())
    6. {
    7. setRootIndex(listingIndex);
    8. show();
    9. }
    10. }
    11.  
    12. delete wizard;
    13. wizard = 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    So in this particular case, delete wizard failed and I have a crash. Moreover, it is silly than some code class code could still be executed after the destruction of the object !!!

    I have a solution (hope it could help) but I would like to know if there is a better way to deal with it.

    Here is my solution by overidding the close() slot of the QMainWindow :

    Qt Code:
    1. void MyWindow::close()
    2. {
    3. if(wizard)
    4. {
    5. closeRequested = true;
    6. wizard->reject();
    7. }
    8. else
    9. {
    10. QMainWindow::close();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    So if the wizard pointer is not null, i set a member boolean variable to true and I programatically reject the dialog rather than propagating the close() call to the parent (QMainWindow) classe.

    Then, the control returned right after the wizard->exec() call, I can safely destroy it and it test the boolean just after and do my close() call then.

    Do you think there is a much cleaner solution ?

  2. #2
    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: Qt::WA_DeleteOnClose while a child QDialog is executed

    How about something like:
    Qt Code:
    1. connect(window, SIGNAL(destroyed()), wizard, SLOT(reject()));
    To copy to clipboard, switch view to plain text mode 
    or just call QApplication::quit() in the first place?
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    53
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt::WA_DeleteOnClose while a child QDialog is executed

    Quote Originally Posted by jpn View Post
    How about something like:
    Qt Code:
    1. connect(window, SIGNAL(destroyed()), wizard, SLOT(reject()));
    To copy to clipboard, switch view to plain text mode 
    or just call QApplication::quit() in the first place?
    Good idea, I didn't tried this way (but I have tried to call reject() on the wizard directly from my destructor but it was not working) but it did not work as well.

    Curiously I've got this error in the debug output :

    QObject: Do not delete object, 'qt_scrollarea_viewport', during its event handler!

    I have a QScrollArea in my mainwindow but if the wizard is not executed, the destruction process is ok so I really don't think this error is related to the qt_scrollarea_viewport of my main window.

    Perhaps the fact that a QDialog is modal when called with exec() is blocking the event loop of the main window and that it is not really good to destroy the object while the event loop is in this state so not properly destroyed ?

    Anyway, as I told in my first post, I've got a solution, i was just looking for a potential better one ;o)

  4. #4
    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: Qt::WA_DeleteOnClose while a child QDialog is executed

    Quote Originally Posted by nooky59 View Post
    Curiously I've got this error in the debug output :

    QObject: Do not delete object, 'qt_scrollarea_viewport', during its event handler!
    Use QObject::deleteLater() instead of C++ delete operator.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    53
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt::WA_DeleteOnClose while a child QDialog is executed

    Quote Originally Posted by jpn View Post
    Use QObject::deleteLater() instead of C++ delete operator.
    The problem is the delete is triggered automatically by the close() operation as Qt::WA_DeleteOnClose is set.

Similar Threads

  1. Replies: 4
    Last Post: 4th June 2007, 12:07

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.