6.0.0-rc4Source

QDialog Class

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

Header#include <QDialog>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget
Inherited byQColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QProgressDialog, QWizard

List of all members, including inherited members

Public Types

Public Functions

QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
~QDialog()
intresult() const
voidsetResult(int result)
voidsetModal(bool modal)

Reimplemented Public Functions

voidsetVisible(bool visible)

Public Slots

virtual voidopen()
virtual intexec()
virtual voiddone(int r)
virtual voidaccept()
virtual voidreject()

Signals

voidfinished(int result)
voidaccepted()
voidrejected()

Reimplemented Protected Functions

voidkeyPressEvent(QKeyEvent *event)
voidcloseEvent(QCloseEvent *event)

Detailed Description

A dialog is a window for a short task: asking the user something, or showing a message. A dialog with a parent is a window of its own that the window system keeps above the window of its parent, and it is centered over that window when it is first shown.

A modal dialog blocks the input of the other windows while it is shown. exec() shows the dialog as an application-modal dialog and returns when it is closed, with its result:

QDialog dialog(window);
auto *layout = new QVBoxLayout(&dialog);
layout->addWidget(new QLabel("Delete the file?"));
auto *buttons = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No);
layout->addWidget(buttons);
QObject::connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
QObject::connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
if (dialog.exec() == QDialog::Accepted)
    deleteFile();

open() shows the dialog as a window-modal dialog and returns at once; connect to finished() for the result. A dialog shown with show() is modeless unless setModal() was called.

Escape and the close button of the window reject the dialog. Return and Enter click the default button (see QPushButton::setDefault()).

See also QDialogButtonBox, QMessageBox and QWidget::setWindowModality().

Differences from Qt

There is no size grip (setSizeGripEnabled()), no extension (setExtension()) and no Qt::WA_DeleteOnClose. A modal dialog blocks the mouse and the keyboard of the windows behind it, but they are not shown as blocked and clicking them does not raise the dialog.

Member Type Documentation

enum QDialog::DialogCode

The results that accept() and reject() give a dialog.

ConstantDescription
QDialog::RejectedThe dialog was rejected: cancelled or closed.
QDialog::AcceptedThe dialog was accepted.

Member Function Documentation

[explicit] QDialog::QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())

Constructs a dialog with the given parent and window flags f. Without a kind of window in f the dialog is a Qt::Dialog.

[override] QDialog::~QDialog()

Destroys the dialog. A dialog that is destroyed while exec() runs makes exec() return Rejected.

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

Closes the dialog with the result Accepted.

See also done() and reject().

[signal] void QDialog::accepted()

This signal is emitted when the dialog is accepted, before finished().

[override, protected] void QDialog::closeEvent(QCloseEvent *event)

Reimplements QWidget::closeEvent().

Closing the window rejects the dialog if it is still visible.

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

Closes the dialog with the result r: hides it, sets the result, emits accepted() or rejected() if r is Accepted or Rejected, and then finished(). exec() returns r.

See also accept() and reject().

[slot, virtual] int QDialog::exec()

Shows the dialog as an application-modal dialog and blocks until it closes, running an event loop meanwhile. Returns the result of the dialog, or Rejected if it was destroyed. A dialog that is already in exec() returns -1.

See also open() and result().

[signal] void QDialog::finished(int result)

This signal is emitted when the dialog is closed by done(), accept() or reject(), or by the user, with the result it closed with.

See also accepted() and rejected().

[override, protected] void QDialog::keyPressEvent(QKeyEvent *event)

Reimplements QWidget::keyPressEvent().

Escape rejects the dialog. Return and Enter click the default button, if it is enabled.

[slot, virtual] void QDialog::open()

Shows the dialog as a window-modal dialog and returns at once. The modality is reset when the dialog closes. Connect to finished() to get the result.

See also exec() and finished().

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

Closes the dialog with the result Rejected.

See also done() and accept().

[signal] void QDialog::rejected()

This signal is emitted when the dialog is rejected, before finished(). Escape and the close button of the window reject the dialog.

int QDialog::result() const

Returns the result of the dialog: usually Accepted or Rejected, or the code given to done().

See also setResult().

void QDialog::setModal(bool modal)

Makes the dialog application-modal if modal is true, and modeless otherwise. It takes effect the next time the dialog is shown with show().

See also QWidget::setWindowModality() and exec().

void QDialog::setResult(int result)

Sets the result of the dialog to result.

See also result().

[override] void QDialog::setVisible(bool visible)

Reimplements QWidget::setVisible().

The first time the dialog is shown it takes its size hint, unless it was resized, and is centered over the window of its parent, or on the screen if it has no parent, unless it was moved. Hiding the dialog ends exec().