Qt logo

QCloseEvent Class Reference


The QCloseEvent class contains parameters that describe a close event. More...

#include <qevent.h>

Inherits QEvent.

List of all member functions.

Public Members


Detailed Description

The QCloseEvent class contains parameters that describe a close event.

Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu. They are also sent when you call QWidget::close() to close a widget from inside the program.

Close events contain a special accept flag which tells whether the receiver wants the widget to be closed. When a widget accepts the close event, it is hidden. If it refuses to accept the close event, nothing happens.

The main widget of the application is a special case. When it accepts the close event, the application is immediately terminated.

The event handler QWidget::closeEvent() receives close events.

The default implementation of this event handler accepts the close event. This makes Qt hide the widget.

    void QWidget::closeEvent( QCloseEvent *e )
    {
        e->accept();                            // hides the widget
    }

If you do not want your widget to be hidden, you should reimplement the event handler.

    void MyWidget::closeEvent( QCloseEvent *e )
    {
        e->ignore();                            // does not hide the widget
    }

A typical reimplementation of a close event handler is shown in the qwerty/qwerty.cpp example: If the document wasn't changed, the close event is accepted with e->except(). If there are unsaved changes, it asks the user whether she wants to save the data. This is done with QMessageBox::warning(). The close event is only accepted if the data was either saved successfully or if the user explicitely stated that the modifications shall be discarded.

If you want your widget also to be deleted when it is closed, simply create it with the WDestructiveClose widget flag. This is very useful for the independent toplevel windows of a multi window application. The qwerty/qwerty.cpp example also makes use of this.

Warning: Be careful. destructive close implies that the widget was created on the heap using the new operator. Even when the widget has been created by new doing this is a tricky operation. Be sure that you cannot have any other pointers to the widget hanging around.

QObject emits the destroyed() signal when it is deleted. This is a useful signal if a widget needs to know when another widget is deleted.

If the last toplevel window is closed, the QApplication::lastWindowClosed() signal is emitted.

See also: QWidget::close(), QWidget::hide(), QObject::destroyed(), QApplication::setMainWidget(), QApplication::lastWindowClosed() and QApplication::quit().

Examples: popup/popup.cpp


Member Function Documentation

QCloseEvent::QCloseEvent ()

Constructs a close event object with the accept parameter flag set to FALSE.

void QCloseEvent::accept ()

Sets the accept flag of the close event object.

Setting the accept flag indicates that the receiver of this event agrees to close the widget.

The accept flag is not set by default.

If you choose to accept in QWidget::closeEvent(), the widget will be hidden. If the widget was created with the WDestructiveClose widget flag, it is also destroyed.

See also: ignore() and QWidget::hide().

Examples: popup/popup.cpp

void QCloseEvent::ignore ()

Clears the accept flag of the close event object.

Clearing the accept flag indicates that the receiver of this event does not want the widget to be closed.

The accept flag is not set by default.

See also: accept().

bool QCloseEvent::isAccepted () const

Returns TRUE if the receiver of the event has agreed to close the widget.

See also: accept() and ignore().


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.


Copyright © 1999 Troll TechTrademarks
Qt version 2.0.2