
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQDialog class is the base class of dialog windows.More...
| Header: | #include <QDialog> |
| Inherits: | QWidget |
| Inherited By: | Q3FileDialog,Q3ProgressDialog,Q3TabDialog,Q3Wizard,QAbstractPrintDialog,QColorDialog,QErrorMessage,QFileDialog,QFontDialog,QInputDialog,QMessageBox,QPageSetupDialog,QPrintPreviewDialog,QProgressDialog, andQWizard |
| enum | DialogCode { Accepted, Rejected } |
| QDialog(QWidget * parent = 0, Qt::WindowFlags f = 0) | |
| ~QDialog() | |
| bool | isSizeGripEnabled() const |
| int | result() const |
| void | setModal(bool modal) |
| void | setResult(int i) |
| void | setSizeGripEnabled(bool) |
| virtual QSize | minimumSizeHint() const |
| virtual void | setVisible(bool visible) |
| virtual QSize | sizeHint() const |
| virtual void | closeEvent(QCloseEvent * e) |
| virtual void | contextMenuEvent(QContextMenuEvent * e) |
| virtual bool | event(QEvent * e) |
| virtual bool | eventFilter(QObject * o, QEvent * e) |
| virtual void | keyPressEvent(QKeyEvent * e) |
| virtual void | resizeEvent(QResizeEvent *) |
| virtual void | showEvent(QShowEvent * event) |
TheQDialog class is the base class of dialog windows.
A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide areturn value, and they can havedefault buttons. QDialogs can also have aQSizeGrip in their lower-right corner, usingsetSizeGripEnabled().
Note thatQDialog (and any other widget that has typeQt::Dialog) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar entry.
Use the overload of theQWidget::setParent() function to change the ownership of aQDialog widget. This function allows you to explicitly set the window flags of the reparented widget; using the overloaded function will clear the window flags specifying the window-system properties for the widget (in particular it will reset theQt::Dialog flag).
Amodal dialog is a dialog that blocks input to other visible windows in the same application. Dialogs that are used to request a file name from the user or that are used to set application preferences are usually modal. Dialogs can beapplication modal (the default) orwindow modal.
When an application modal dialog is opened, the user must finish interacting with the dialog and close it before they can access any other window in the application. Window modal dialogs only block access to the window associated with the dialog, allowing the user to continue to use other windows in an application.
The most common way to display a modal dialog is to call itsexec() function. When the user closes the dialog,exec() will provide a usefulreturn value. Typically, to get the dialog to close and return the appropriate value, we connect a default button, e.g.OK, to theaccept() slot and aCancel button to thereject() slot. Alternatively you can call thedone() slot withAccepted orRejected.
An alternative is to callsetModal(true) orsetWindowModality(), thenshow(). Unlikeexec(),show() returns control to the caller immediately. CallingsetModal(true) is especially useful for progress dialogs, where the user must have the ability to interact with the dialog, e.g. to cancel a long running operation. If you useshow() andsetModal(true) together to perform a long operation, you must callQApplication::processEvents() periodically during processing to enable the user to interact with the dialog. (SeeQProgressDialog.)
Amodeless dialog is a dialog that operates independently of other windows in the same application. Find and replace dialogs in word-processors are often modeless to allow the user to interact with both the application's main window and with the dialog.
Modeless dialogs are displayed usingshow(), which returns control to the caller immediately.
If you invoke theshow() function after hiding a dialog, the dialog will be displayed in its original position. This is because the window manager decides the position for windows that have not been explicitly placed by the programmer. To preserve the position of a dialog that has been moved by the user, save its position in yourcloseEvent() handler and then move the dialog to that position, before showing it again.
A dialog'sdefault button is the button that's pressed when the user presses Enter (Return). This button is used to signify that the user accepts the dialog's settings and wants to close the dialog. UseQPushButton::setDefault(),QPushButton::isDefault() andQPushButton::autoDefault() to set and control the dialog's default button.
If the user presses the Esc key in a dialog,QDialog::reject() will be called. This will cause the window to close: Theclose event cannot beignored.
Extensibility is the ability to show the dialog in two ways: a partial dialog that shows the most commonly used options, and a full dialog that shows all the options. Typically an extensible dialog will initially appear as a partial dialog, but with aMore toggle button. If the user presses theMore button down, the dialog is expanded. TheExtension Example shows how to achieve extensible dialogs using Qt.
Modal dialogs are often used in situations where a return value is required, e.g. to indicate whether the user pressedOK orCancel. A dialog can be closed by calling theaccept() or thereject() slots, andexec() will returnAccepted orRejected as appropriate. Theexec() call returns the result of the dialog. The result is also available fromresult() if the dialog has not been destroyed.
In order to modify your dialog's close behavior, you can reimplement the functionsaccept(),reject() ordone(). ThecloseEvent() function should only be reimplemented to preserve the dialog's position or to override the standard close or reject behavior.
A modal dialog:
void EditorWindow::countWords(){ WordCountDialog dialog(this); dialog.setWordCount(document().wordCount()); dialog.exec();}
A modeless dialog:
void EditorWindow::find(){if (!findDialog) { findDialog=new FindDialog(this); connect(findDialog, SIGNAL(findNext()),this, SLOT(findNext())); } findDialog->show(); findDialog->raise(); findDialog->activateWindow();}
See alsoQDialogButtonBox,QTabWidget,QWidget,QProgressDialog,GUI Design Handbook: Dialogs, Standard,Extension Example, andStandard Dialogs Example.
The value returned by a modal dialog.
| Constant | Value |
|---|---|
QDialog::Accepted | 1 |
QDialog::Rejected | 0 |
This property holds whether show() should pop up the dialog as modal or modeless.
By default, this property is false andshow() pops up the dialog as modeless. Setting his property to true is equivalent to settingQWidget::windowModality toQt::ApplicationModal.
exec() ignores the value of this property and always pops up the dialog as modal.
Access functions:
| bool | isModal() const |
| void | setModal(bool modal) |
See alsoQWidget::windowModality,show(), andexec().
This property holds whether the size grip is enabled.
AQSizeGrip is placed in the bottom-right corner of the dialog when this property is enabled. By default, the size grip is disabled.
Access functions:
| bool | isSizeGripEnabled() const |
| void | setSizeGripEnabled(bool) |
Constructs a dialog with parentparent.
A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent. It will also share the parent's taskbar entry.
The widget flagsf are passed on to theQWidget constructor. If, for example, you don't want a What's This button in the title bar of the dialog, passQt::WindowTitleHint |Qt::WindowSystemMenuHint inf.
See alsoQWidget::setWindowFlags().
Destroys theQDialog, deleting all its children.
[virtual slot]void QDialog::accept()Hides the modal dialog and sets the result code toAccepted.
[signal]void QDialog::accepted()This signal is emitted when the dialog has been accepted either by the user or by callingaccept() ordone() with theQDialog::Accepted argument.
Note that this signal isnot emitted when hiding the dialog withhide() orsetVisible(false). This includes deleting the dialog while it is visible.
This function was introduced in Qt 4.1.
See alsofinished() andrejected().
[virtual protected]void QDialog::closeEvent(QCloseEvent * e)Reimplemented fromQWidget::closeEvent().
[virtual protected]void QDialog::contextMenuEvent(QContextMenuEvent * e)Reimplemented fromQWidget::contextMenuEvent().
[virtual slot]void QDialog::done(int r)Closes the dialog and sets its result code tor. If this dialog is shown withexec(), done() causes the local event loop to finish, andexec() to returnr.
As withQWidget::close(), done() deletes the dialog if theQt::WA_DeleteOnClose flag is set. If the dialog is the application's main widget, the application terminates. If the dialog is the last window closed, theQApplication::lastWindowClosed() signal is emitted.
See alsoaccept(),reject(),QApplication::activeWindow(), andQApplication::quit().
[virtual protected]bool QDialog::event(QEvent * e)Reimplemented fromQObject::event().
[virtual protected]bool QDialog::eventFilter(QObject * o,QEvent * e)Reimplemented fromQObject::eventFilter().
[slot]int QDialog::exec()Shows the dialog as amodal dialog, blocking until the user closes it. The function returns aDialogCode result.
If the dialog isapplication modal, users cannot interact with any other window in the same application until they close the dialog. If the dialog iswindow modal, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.
See alsoopen(),show(),result(), andsetWindowModality().
[signal]void QDialog::finished(int result)This signal is emitted when the dialog'sresult code has been set, either by the user or by callingdone(),accept(), orreject().
Note that this signal isnot emitted when hiding the dialog withhide() orsetVisible(false). This includes deleting the dialog while it is visible.
This function was introduced in Qt 4.1.
See alsoaccepted() andrejected().
[virtual protected]void QDialog::keyPressEvent(QKeyEvent * e)Reimplemented fromQWidget::keyPressEvent().
[virtual]QSize QDialog::minimumSizeHint() constReimplemented fromQWidget::minimumSizeHint().
[slot]void QDialog::open()Shows the dialog as awindow modal dialog, returning immediately.
This function was introduced in Qt 4.5.
See alsoexec(),show(),result(), andsetWindowModality().
[virtual slot]void QDialog::reject()Hides the modal dialog and sets the result code toRejected.
[signal]void QDialog::rejected()This signal is emitted when the dialog has been rejected either by the user or by callingreject() ordone() with theQDialog::Rejected argument.
Note that this signal isnot emitted when hiding the dialog withhide() orsetVisible(false). This includes deleting the dialog while it is visible.
This function was introduced in Qt 4.1.
See alsofinished() andaccepted().
[virtual protected]void QDialog::resizeEvent(QResizeEvent *)Reimplemented fromQWidget::resizeEvent().
In general returns the modal dialog's result code,Accepted orRejected.
Note:When used fromQMessageBox instance the result code type isQMessageBox::StandardButton
Do not call this function if the dialog was constructed with theQt::WA_DeleteOnClose attribute.
See alsosetResult().
Sets the modal dialog's result code toi.
Note:We recommend that you use one of the values defined byQDialog::DialogCode.
See alsoresult().
[virtual]void QDialog::setVisible(bool visible)Reimplemented fromQWidget::setVisible().
[virtual protected]void QDialog::showEvent(QShowEvent * event)Reimplemented fromQWidget::showEvent().
[virtual]QSize QDialog::sizeHint() constReimplemented fromQWidget::sizeHint().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.