PDA

View Full Version : Delete/Close Dialog correct?



kei
3rd June 2009, 19:40
Hi,
i have two question:

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


Dialog *d = new Dialog(this);
d->deleteLater();
d->exec();

d->getSomething();

I thought that deleteLater() will remove d after leaving the dialog, but the methode getSomething() still works :confused: 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

wysota
3rd June 2009, 19:58
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.

kei
3rd June 2009, 20:44
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

d->getSomething();
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() :eek:

wysota
3rd June 2009, 21:18
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() :eek:
Sorry, I meant finished().

Boy
1st July 2009, 16:11
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?

wysota
1st July 2009, 16:48
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:

Dialog object;
object.exec();
... or ...

Dialog *object = new Dialog;
object->setAttribute(Qt::WA_DeleteOnClose);
object->exec();

Boy
2nd July 2009, 09:32
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:

Dialog object;
object.exec();
... or ...

Dialog *object = new Dialog;
object->setAttribute(Qt::WA_DeleteOnClose);
object->exec();

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?

wysota
2nd July 2009, 09:59
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.

Boy
2nd July 2009, 13:51
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!