Qt logo

QDialog Class Reference


The QDialog class is the base class of dialog windows. More...

#include <qdialog.h>

Inherits QWidget.

Inherited by QColorDialog, QFileDialog, QFontDialog, QMessageBox, QTabDialog and QWizard.

List of all member functions.

Public Members

Protected Members

Protected Slots


Detailed Description

The QDialog class is the base class of dialog windows.

A dialog window is a widget used to communicate with the user. It offers mechanisms such as default buttons.

The dialog window can either be modeless or modal. A modeless dialog is a normal window, while a modal window must be finished before the user can continue with other parts of the program. The third constructor argument must be set to TRUE to create a modal dialog, otherwise it will create a modeless dialog.

Example (your own modal dialog):

    class Modal : public QDialog {
        Q_OBJECT
    public:
        Modal( QWidget *parent, QString name );
    };

    Modal::Modal( QWidget *parent, QString name )
        : QDialog( parent, name, TRUE )
    {
        QPushButton *ok, *cancel;
        ok = new QPushButton( "OK", this );
        ok->setGeometry( 10,10, 100,30 );
        connect( ok, SIGNAL(clicked()), SLOT(accept()) );
        cancel = new QPushButton( "Cancel", this );
        cancel->setGeometry( 10,60, 100,30 );
        connect( cancel, SIGNAL(clicked()), SLOT(reject()) );
    }

Note that the parent widget has an additional meaning for modal dialogs. A modal dialog is placed on top of the parent widget. The dialog is centered on the screen if the parent widget is zero.

You would normally call exec() to start a modal dialog. This enters a local event loop, which is terminated when the modal dialog calls done() (or accept() or reject()).

Example (using a modal dialog):

    Modal m;
    if ( m.exec() ) {
       // ok was pressed, then fetch the interesting dialog data
    }

Modeless dialogs behave just like ordinary toplevel widgets. The only difference is that they have the default button mechanism.

See also: QTabDialog, QWidget, QSemiModal and GUI Design Handbook: Dialogs, Standard.


Member Function Documentation

QDialog::QDialog ( QWidget * parent=0, const char * name=0, bool modal=FALSE, WFlags f=0 )

Constructs a dialog named name, which has a parent widget parent.

The dialog will by default be modeless, unless you set modal to TRUE to construct a modal dialog.

The f argument is the widget flags, which can be used to customize the window frame style.

A dialog is always a top level widget. The optional parent, however, will know about this child and also delete it on destruction. Furthermore, the window system will be able to tell that both the dialog and the parent belong together. This works for Windows and also some X11 window managers, that will for instance provide a common taskbar entry in that case.

It is recommended to pass a parent.

QDialog::~QDialog ()

Destroys the QDialog and all its children.

void QDialog::accept () [virtual protected slot]

Closes the dialog and sets the result code to Accepted.

Equivalent to done(Accepted);.

void QDialog::closeEvent ( QCloseEvent * e ) [virtual protected]

Calls reject() if it is a modal dialog, or accepts the close event if it is a modeless dialog.

Reimplemented from QWidget.

void QDialog::done ( int r ) [virtual protected slot]

Closes the dialog and sets the result code to r.

Equivalent to calling hide(), then setResult(r ).

This function is very useful for modal dialogs. It leaves the local event loop and returns from the exec() or show() function.

Warning: Although done() will return to the caller also if this dialog is modal, the local event loop is then marked for termination. Hence, a program should not try to do anything that depends on event handling before the corresponding exec() or show() has returned.

See also: accept() and reject().

int QDialog::exec ()

For modal dialogs: Starts the dialog and returns the result code.

Equivalent to calling show(), then result().

This function is very useful for modal dialogs, but makes no sense for modeless dialog. It enters a new local event loop. The event loop is terminated when the dialog is hidden, usually by calling done().

A warning message is printed if you call this function for a modeless dialog.

See also: show() and result().

Examples: qdir/qdir.cpp wizard/main.cpp

void QDialog::keyPressEvent ( QKeyEvent * e ) [virtual protected]

Handles key press events for the dialog.

Calls reject() if Escape is pressed. Simulates a button click for the default button if Enter is pressed. All other keys are ignored.

Reimplemented from QWidget.

void QDialog::move ( const QPoint & p )

Reimplements QWidget::move() for internal purposes.

void QDialog::move ( int x, int y ) [virtual]

Reimplements QWidget::move() for internal purposes.

Reimplemented from QWidget.

void QDialog::reject () [virtual protected slot]

Closes the dialog and sets the result code to Rejected.

Equivalent to done(Rejected);.

void QDialog::resize ( const QSize & s )

Reimplements QWidget::resize() for internal purposes.

void QDialog::resize ( int w, int h ) [virtual]

Reimplements QWidget::resize() for internal purposes.

Reimplemented from QWidget.

int QDialog::result () const

Returns the result code of the dialog.

void QDialog::setGeometry ( const QRect & r ) [virtual]

Reimplements QWidget::setGeometry() for internal purposes.

Reimplemented from QWidget.

void QDialog::setGeometry ( int x, int y, int w, int h ) [virtual]

Reimplements QWidget::setGeometry() for internal purposes.

Reimplemented from QWidget.

void QDialog::setResult ( int ) [protected]

Sets the result code of the dialog.

void QDialog::show () [virtual]

Shows the dialog box on the screen, as QWidget::show() and enters a local event loop if this dialog is modal (see constructor).

This implementation also does automatic resizing and automatic positioning. If you have not already resized or moved the dialog, it will find a size that fits the contents and a position near the middle of the screen (or centered relative to the parent widget if any).

Warning: Calling show() for a modal dialog enters a local event loop. The event loop is terminated when the dialog is hidden, usually by calling done().

See also: exec().

Examples: movies/main.cpp

Reimplemented from QWidget.


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