Qt Toolkit - QDialog Class

# QDialog Class Reference

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

`#include <qdialog.h>`

Inherits QWidget.

Inherited by QFileDialog, QMessageBox, QPrintDialog and QTabDialog.

List of all member functions.

## Public Members

- QDialog ( QWidget * parent=0, const char * name=0, bool modal=FALSE, WFlags f=0 )
- ~QDialog ()
- int exec ()
- int result () const
- virtual void show ()
- virtual void move ( int x, int y )
- void move ( const QPoint & p )
- virtual void resize ( int w, int h )
- void resize ( const QSize & )
- virtual void setGeometry ( int x, int y, int w, int h )
- void setGeometry ( const QRect & )

## Protected Members

- void setResult ( int r )
- virtual void keyPressEvent ( QKeyEvent * )
- virtual void closeEvent ( QCloseEvent * )

## Protected Slots

- virtual void done ( int )
- void accept ()
- void reject ()

## 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, const char *name );
        };

        Modal::Modal( QWidget *parent, const char *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 a different meaning for modal dialogs than for other types of widgets. A 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 widgets. The only difference is that they have the default button mechanism.

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

Examples: xform/xform.cpp

## 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 contruct a modal dialog.

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

Warning: Creating a modeless dialog with a parent makes it an ordinary child widget, which is probably not what you want. Expect strange behavior (QDialog has a default button mechanism).

### QDialog::~QDialog ()

Destroys the QDialog and all its children.

### void QDialog::accept () `[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 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 ()

Starts the dialog and returns the result code.

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

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

### 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 () `[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.

Examples: pref/pref.cpp

### 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 )

Reimplements QWidget::setGeometry() for internal purposes.

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

Reimplements QWidget::setGeometry() for internal purposes.

Examples: xform/xform.cpp

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 xform/xform.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.

It was generated from the following files:

- qdialog.h: 1998/08/21
- qdialog.cpp: 1998/08/26