[Qt-interest] QDialog::closeEvent() is called only when a modal dialog is closed by clicking the "X" button

Constantin Makshin cmakshin at gmail.com
Tue Jun 28 17:13:53 CEST 2011


Closing a window causes it to be destroyed only when the Qt::WA_DeleteOnClose attribute is set. And it's turned off by default, so close()/done() won't make your pointer invalid unless you explicitly tell it to do so (by enabling that flag).

On Tuesday, June 28, 2011 06:45:01 PM Andre Somers wrote:
> Hi,
> 
> On Tue, June 28, 2011 4:35 pm, Constantin Makshin wrote:
> > Well, after paying a bit more attention to QDialog documentation I've
> > found that accept() and reject() slots *hide* the dialog instead of
> > closing it. But these slots' "friend" — the done() slot — *closes* the
> > dialog (according to the documentation). So the behavior still looks weird
> > as IMHO one would expect that accept() and reject() are simply convenience
> > wrappers around done() and therefore the dialog should be closed, not just
> > hidden...
> >
> This behavior is totally logical.
> If accept and reject would actually destroy the object, it would be
> impossible to use code to retrieve values from the dialog afterwards. The
> pointer to the dialog would have become invalid behind your back!
> 
> André
> 
> 
> > As a workaround, I've used the event() function and put the parameter
> > saving code into the "if (event->type() == QEvent::Close ||
> > event()->type() == QEvent::Hide)" block. But I'd appreciate more detailed
> > information about QDialog's "close vs. hide" question.
> >
> > On Tuesday, June 28, 2011 06:08:07 PM Zeljko wrote:
> >> Constantin Makshin wrote:
> >>
> >> > Hello!
> >> >
> >> > I want to save some modal dialog's parameters when it's closed and
> >> restore
> >> > them when it's created/opened again. The obvious way is to save
> >> parameters
> >> > inside the closeEvent() function and restore inside the constructor.
> >> This
> >> > approach works for QMainWindow derivatives but has some weird behavior
> >> for
> >> > modal dialogs — closeEvent() is called only when the dialog is
> >> closed by
> >> > clicking the "X" button or choosing corresponding window context menu
> >> item. If
> >> > the user presses "OK" or "Cancel" button (or takes some other action
> >> that's
> >> > connected to accept() or reject()), closeEvent() is not called, making
> >> the
> >> > dialog-parameter-saving idea mostly useless.
> >> >
> >> > Is this a bug or intended behavior?
> >>
> >> I guess it's bug, since closeEvent() should be passed before real
> >> closing ...and
> >> there you can stop it. eg. "Do you really want to close this window (no
> >> matter
> >> that's modal window or dialog or whatever" ...especially if that
> >> triggers on
> >> close button ("X").
> >>
> >> zeljko
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110628/249b13b3/attachment.bin 


More information about the Qt-interest-old mailing list