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:
Code:
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
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.
Re: Delete/Close Dialog correct?
Quote:
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
still?
Quote:
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:
Re: Delete/Close Dialog correct?
Quote:
Originally Posted by
kei
That means that the dialog becomes deleted after exec() returns?
No. It means it will get deleted when the flow returns to QCoreApplication::exec().
Quote:
I don't care^^
But close() is a slot there is no signal closed() :eek:
Sorry, I meant finished().
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?
Re: Delete/Close Dialog correct?
Quote:
Originally Posted by
Boy
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.
Quote:
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.
Quote:
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:
Code:
Dialog object;
object.exec();
... or ...
Code:
Dialog *object = new Dialog;
object->setAttribute(Qt::WA_DeleteOnClose);
object->exec();
Re: Delete/Close Dialog correct?
Quote:
Originally Posted by
wysota
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:
Code:
Dialog object;
object.exec();
... or ...
Code:
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?
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.
Re: Delete/Close Dialog correct?
Quote:
Originally Posted by
wysota
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!