Results 1 to 9 of 9

Thread: Delete/Close Dialog correct?

  1. #1
    Join Date
    May 2009
    Location
    Germany
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Delete/Close Dialog correct?

    Hi,
    i have two question:

    First i have the following code (from a example) and i'm not sure why this works:

    Qt Code:
    1. Dialog *d = new Dialog(this);
    2. d->deleteLater();
    3. d->exec();
    4.  
    5. d->getSomething();
    To copy to clipboard, switch view to plain text mode 

    I thought that deleteLater() will remove d after leaving the dialog, but the methode getSomething() still works I'm sure its only a little misunderstanding. Could you explain me this?

    Second i leave this dialog with a button which is connected to the dialog's close() slot. Is this the right way?

    greetings,
    kei

  2. #2
    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: Delete/Close Dialog correct?

    It will delete the dialog once the control returns to the main event loop. The dialog's event loop has deferred delete capabilities disabled.

    You should listen to accepted() and rejected() signals if you care how the dialog was closed. If you don't care then closed() signal(!) is fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Boy (2nd July 2009)

  4. #3
    Join Date
    May 2009
    Location
    Germany
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delete/Close Dialog correct?

    It will delete the dialog once the control returns to the main event loop. The dialog's event loop has deferred delete capabilities disabled.
    That means that the dialog becomes deleted after exec() returns? But why works
    Qt Code:
    1. d->getSomething();
    To copy to clipboard, switch view to plain text mode 
    still?

    You should listen to accepted() and rejected() signals if you care how the dialog was closed. If you don't care then closed() signal(!) is fine.
    I don't care^^
    But close() is a slot there is no signal closed()

  5. #4
    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: Delete/Close Dialog correct?

    Quote Originally Posted by kei View Post
    That means that the dialog becomes deleted after exec() returns?
    No. It means it will get deleted when the flow returns to QCoreApplication::exec().

    I don't care^^
    But close() is a slot there is no signal closed()
    Sorry, I meant finished().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    Boy (2nd July 2009)

  7. #5
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delete/Close Dialog correct?

    So to be sure:

    deleteLater() can be used to delete an QDialog when it is closed?

    Am I correct that this is to be used so that all slots that are called when the dialog is closed are handled before the object becomes invalid?

    The way described by the topic starter:

    object->deleteLater();
    object->exec(); // stuff shown in dialog
    // dialog closed, function ends and don't worry further, dialog will be deleted?

    or is it better to connect the finished() signal to the deleteLater() slot?

  8. #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: Delete/Close Dialog correct?

    Quote Originally Posted by Boy View Post
    deleteLater() can be used to delete an QDialog when it is closed?
    It depends when you call it. But one way or the other deleting the widget causes it to close so the effect will be the same.

    Am I correct that this is to be used so that all slots that are called when the dialog is closed are handled before the object becomes invalid?
    Among other things, yes.

    The way described by the topic starter:

    object->deleteLater();
    object->exec(); // stuff shown in dialog
    // dialog closed, function ends and don't worry further, dialog will be deleted?
    This will work but only because QDialog::exec() works in a special mode where it doesn't process deferred deletes.
    The proper way to do it is either like so:
    Qt Code:
    1. Dialog object;
    2. object.exec();
    To copy to clipboard, switch view to plain text mode 
    ... or ...
    Qt Code:
    1. Dialog *object = new Dialog;
    2. object->setAttribute(Qt::WA_DeleteOnClose);
    3. object->exec();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    Boy (2nd July 2009)

  10. #7
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delete/Close Dialog correct?

    Quote Originally Posted by wysota View Post
    It depends when you call it. But one way or the other deleting the widget causes it to close so the effect will be the same.


    Among other things, yes.


    This will work but only because QDialog::exec() works in a special mode where it doesn't process deferred deletes.
    The proper way to do it is either like so:
    Qt Code:
    1. Dialog object;
    2. object.exec();
    To copy to clipboard, switch view to plain text mode 
    ... or ...
    Qt Code:
    1. Dialog *object = new Dialog;
    2. object->setAttribute(Qt::WA_DeleteOnClose);
    3. object->exec();
    To copy to clipboard, switch view to plain text mode 
    Well, my problem is that I have a dialog which is in fact a popup. I have the following flags set:

    Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Popup

    Now I want, when the user clicks somewhere else than on the popup itself, that it disappears and deletes.

    When I use exec(), it is modal, so can't click somewhere else to make it disappear.
    So I use show() which does do this, but then the closeEvent is not sent, so it doesn't delete itself when the WA_DeleteOnClose has been set? However, finished() signal is send, so maybe I can do a close slot on this?

  11. #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: Delete/Close Dialog correct?

    If you have a popup then you shouldn't base it on QDialog but rather QWidget. Then set DeleteOnClose and call show(). Everything should work fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    Boy (2nd July 2009)

  13. #9
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delete/Close Dialog correct?

    Quote Originally Posted by wysota View Post
    If you have a popup then you shouldn't base it on QDialog but rather QWidget. Then set DeleteOnClose and call show(). Everything should work fine.
    Great! This is exactly what I needed...tested it with connecting a slot to the destroyed() signal of the widget and it got called...

    Thanks again!

Similar Threads

  1. Issue with Modelless dialog on Mac
    By Satyanarayana Chebrolu in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2009, 10:10
  2. Dialog is not closing
    By manmohan in forum Newbie
    Replies: 5
    Last Post: 1st December 2008, 17:04
  3. Replies: 9
    Last Post: 13th August 2008, 18:07
  4. Resizing the dialog boxes
    By anju123 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2007, 10:41
  5. QGraphicsView: Dialog Position Doubt
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2007, 17:48

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.