QDialog 类

QDialog 类是对话框窗口的基类。 更多...

头: #include <QDialog>
qmake: QT += widgets
继承: QWidget
继承者:

QAbstractPrintDialog , QAxSelect , QColorDialog , QErrorMessage , QFileDialog , QFontDialog , QInputDialog , QMessageBox , QPageSetupDialog , QPrintPreviewDialog , QProgressDialog ,和 QWizard

公共类型

enum DialogCode { Accepted, Rejected }

特性

公共函数

QDialog (QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
~QDialog ()
bool isSizeGripEnabled () const
int result () const
void setModal (bool modal )
void setResult (int i )
void setSizeGripEnabled ( bool )

重实现公共函数

virtual QSize minimumSizeHint () const override
virtual void setVisible (bool visible ) override
virtual QSize sizeHint () const override

公共槽

virtual void accept ()
virtual void done (int r )
virtual int exec ()
virtual void open ()
virtual void reject ()

信号

void accepted ()
void finished (int result )
void rejected ()

重实现保护函数

virtual void closeEvent (QCloseEvent * e ) override
virtual void contextMenuEvent (QContextMenuEvent * e ) override
virtual bool eventFilter (QObject * o , QEvent * e ) override
virtual void keyPressEvent (QKeyEvent * e ) override
virtual void resizeEvent ( QResizeEvent * ) override
virtual void showEvent (QShowEvent * event ) override

额外继承成员

详细描述

QDialog 类是对话框窗口的基类。

对话框窗口是顶级窗口,主要用于短期任务和与用户简短交流。QDialogs 可以是模态 (或非模态) 的。QDialogs 可以提供 返回值 ,且它们可以有 默认按钮 。QDialog 也可拥有 QSizeGrip 在其右下角,使用 setSizeGripEnabled ().

注意, QDialog (和任何其它 Widget 拥有类型 Qt::Dialog ) 使用父级 Widget 与其它 Qt 类稍有不同。对话框始终是顶层 Widget,但若它有父级,其默认位置是居中父级的顶层 Widget 顶部 (若它本身不在顶层)。它还会共享父级的任务栏条目。

使用重载 QWidget::setParent () 函数改变所有权为 QDialog 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 the Qt::Dialog 标志)。

模态对话框

模态 对话框是在同一应用程序中阻塞其它可见窗口输入的对话框。用于从用户请求文件名 (或用于设置应用程序首选项) 的通常是模态对话框。对话框可以为 应用程序模态 (默认) 或 窗口模态 .

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 its exec () function. When the user closes the dialog, exec () will provide a useful 返回值 . Typically, to get the dialog to close and return the appropriate value, we connect a default button, e.g. OK , to the accept () slot and a Cancel button to the reject () slot. Alternatively you can call the done () slot with 接受 or Rejected .

An alternative is to call setModal (true) or setWindowModality (), then show (). Unlike exec (), show () returns control to the caller immediately. Calling setModal (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 use show () 和 setModal (true) together to perform a long operation, you must call QApplication::processEvents () periodically during processing to enable the user to interact with the dialog. (See QProgressDialog .)

非模态对话框

非模态 对话框是独立于同一应用程序中其它窗口运转的对话框。文字处理程序中的查找 替换对话框经常是非模态的,以允许用户与应用程序主窗口和对话框进行交互。

非模态对话框被显示使用 show (),它把控制立即返回给调用者。

若援引 show() 函数在隐藏对话框之后,对话框将显示在其原始位置。这是因为窗口管理器决定程序员未明确放置的窗口位置。要保留用户已移动对话框的位置,把其位置保存在 closeEvent() 处理程序,然后把对话框移动到该位置,在再次展示它之前。

默认按钮

对话框的 default 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. Use QPushButton::setDefault (), QPushButton::isDefault () 和 QPushButton::autoDefault () to set and control the dialog's default button.

Esc 键

若用户在对话框中按下 Esc 键, QDialog::reject () 会被调用。这将导致窗口被关闭: 关闭事件 不可以是 ignored .

可扩展性

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 a 更多 toggle button. If the user presses the 更多 button down, the dialog is expanded. The 扩展范例 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 pressed OK or Cancel . A dialog can be closed by calling the accept () 或 reject () slots, and exec () 会返回 接受 or Rejected as appropriate. The exec () call returns the result of the dialog. The result is also available from result () if the dialog has not been destroyed.

In order to modify your dialog's close behavior, you can reimplement the functions accept (), reject () 或 done ()。 closeEvent() function should only be reimplemented to preserve the dialog's position or to override the standard close or reject behavior.

代码范例

模态对话框:

void EditorWindow::countWords()
{
    WordCountDialog dialog(this);
    dialog.setWordCount(document().wordCount());
    dialog.exec();
}
					

非模态对话框:

void EditorWindow::find()
{
    if (!findDialog) {
        findDialog = new FindDialog(this);
        connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext()));
    }
    findDialog->show();
    findDialog->raise();
    findDialog->activateWindow();
}
					

另请参阅 QDialogButtonBox , QTabWidget , QWidget , QProgressDialog , GUI Design Handbook: Dialogs, Standard , 扩展范例 ,和 标准对话框范例 .

成员类型文档编制

enum QDialog:: DialogCode

由模态对话框返回的值。

常量
QDialog::Accepted 1
QDialog::Rejected 0

特性文档编制

此特性保持是否 show () should pop up the dialog as modal or modeless

默认情况下此特性为 false and show () pops up the dialog as modeless. Setting this property to true is equivalent to setting QWidget::windowModality to Qt::ApplicationModal .

exec () ignores the value of this property and always pops up the dialog as modal.

访问函数:

bool isModal () const
void setModal (bool modal )

另请参阅 QWidget::windowModality , show (),和 exec ().

sizeGripEnabled : bool

此特性保持是否启用大小握把

QSizeGrip 被放置在对话框右下角,当此属性被启用时。默认情况下,大小握把是禁用的。

访问函数:

bool isSizeGripEnabled () const
void setSizeGripEnabled ( bool )

成员函数文档编制

QDialog:: QDialog ( QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())

构造对话框采用父级 parent .

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 flags f are passed on to the QWidget constructor. If, for example, you don't want a What's This button in the title bar of the dialog, pass Qt::WindowTitleHint | Qt::WindowSystemMenuHint in f .

另请参阅 QWidget::setWindowFlags ().

QDialog:: ~QDialog ()

销毁 QDialog , deleting all its children.

[virtual slot] void QDialog:: accept ()

隐藏模态对话框并将结果代码设为 Accepted .

另请参阅 reject () 和 done ().

[signal] void QDialog:: accepted ()

This signal is emitted when the dialog has been accepted either by the user or by calling accept () 或 done () 采用 QDialog::Accepted 自变量。

Note that this signal is not emitted when hiding the dialog with hide () 或 setVisible (false). This includes deleting the dialog while it is visible.

该函数在 Qt 4.1 引入。

另请参阅 finished () 和 rejected ().

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

重实现自 QWidget::closeEvent ().

[override virtual protected] void QDialog:: contextMenuEvent ( QContextMenuEvent * e )

重实现自 QWidget::contextMenuEvent ().

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

关闭对话框并将其结果代码设为 r . If this dialog is shown with exec (), done() causes the local event loop to finish, and exec () to return r .

As with QWidget::close (), done() deletes the dialog if the Qt::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, the QApplication::lastWindowClosed () 信号被发射。

另请参阅 accept (), reject (), QApplication::activeWindow (),和 QCoreApplication::quit ().

[override virtual protected] bool QDialog:: eventFilter ( QObject * o , QEvent * e )

重实现自 QObject::eventFilter ().

[virtual slot] int QDialog:: exec ()

把对话框展示成 模态对话框 ,阻塞直到用户关闭它。函数返回 DialogCode 结果。

若对话框是 应用程序模态 , users cannot interact with any other window in the same application until they close the dialog. If the dialog is 窗口模态 , only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.

另请参阅 open (), show (), result (),和 setWindowModality ().

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

This signal is emitted when the dialog's result code has been set, either by the user or by calling done (), accept (),或 reject ().

Note that this signal is not emitted when hiding the dialog with hide () 或 setVisible (false). This includes deleting the dialog while it is visible.

该函数在 Qt 4.1 引入。

另请参阅 accepted () 和 rejected ().

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

重实现自 QWidget::keyPressEvent ().

[override virtual] QSize QDialog:: minimumSizeHint () const

重实现自 QWidget::minimumSizeHint ().

[virtual slot] void QDialog:: open ()

把对话框展示成 窗口模态对话框 ,立即返回。

该函数在 Qt 4.5 引入。

另请参阅 exec (), show (), result (),和 setWindowModality ().

[virtual slot] void QDialog:: reject ()

隐藏模态对话框并将结果代码设为 Rejected .

另请参阅 accept () 和 done ().

[signal] void QDialog:: rejected ()

This signal is emitted when the dialog has been rejected either by the user or by calling reject () 或 done () 采用 QDialog::Rejected 自变量。

Note that this signal is not emitted when hiding the dialog with hide () 或 setVisible (false). This includes deleting the dialog while it is visible.

该函数在 Qt 4.1 引入。

另请参阅 finished () 和 accepted ().

[override virtual protected] void QDialog:: resizeEvent ( QResizeEvent * )

重实现自 QWidget::resizeEvent ().

int QDialog:: result () const

In general returns the modal dialog's result code, 接受 or Rejected .

注意: When called on a QMessageBox instance, the returned value is a value of the QMessageBox::StandardButton 枚举。

Do not call this function if the dialog was constructed with the Qt::WA_DeleteOnClose 属性。

另请参阅 setResult ().

void QDialog:: setResult ( int i )

Sets the modal dialog's result code to i .

注意: We recommend that you use one of the values defined by QDialog::DialogCode .

另请参阅 result ().

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

重实现自 QWidget::setVisible ().

[override virtual protected] void QDialog:: showEvent ( QShowEvent * event )

重实现自 QWidget::showEvent ().

[override virtual] QSize QDialog:: sizeHint () const

重实现自 QWidget::sizeHint ().