QDialog Class

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

头文件: #include <QDialog>
qmake: QT += widgets
基类: QWidget
派生类:

QColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QProgressDialog, and 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()
  • 19 个公有槽函数继承自 QWidget
  • 1 个公有槽函数继承自 QObject

信号

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
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QDialog 是对话框窗口的基类.

对话框窗口是一个顶级窗口, 主要用于短期任务和与用户的简短通信. QDialog 可以是模态的, 也可以是非模态的. QDialog 可以提供一个 返回值, 且可以有 默认按钮. QDialog 也可以调用setSizeGripEnabled(), 在右下角显示一个 QSizeGrip.

注意: QDialog (及任意带有 Qt::Dialog 标志的 widget) 使用的父 widget 与 Qt 的其他类不同. 对话框始终是一个顶级 widget, 但如果它有一个父级, 它的默认位置将集中在父级的顶级 widget 的顶部 (如果它本身不是顶级的). 它还共享父 widget 的任务栏图标.

使用 QWidget::setParent() 函数修改 QDialog 的所有权. 这个函数允许你显式设置(新的父 widget)窗口标志; 使用这个函数的重载函数将清除对话框的窗口标志 (特别是它将重置 Qt::Dialog 标志).

模态对话框

模态 对话框会阻止同一应用程序中的其他可见窗口获取输入. 获取文件名或应用程序设置首选项的对话框通常是模态的. 对话框的模态可以是 应用程序级别的模态 (默认) 或 窗口基本的模态.

对话框是应用程序级别的模态时, 用户必须完成与对话框的交互并关闭对话框, 然后才能访问应用程序中的任何其他窗口. 窗口级别的模态对话框仅阻止访问与该对话框关联的窗口, 允许用户继续使用应用程序中的其他窗口.

显示模态对话框的最常见方法是调用 exec() 函数. 当用户关闭对话框时, exec() 返回一个有用的 返回值. 通常, 关闭对话框时, 为了返回适当的值, 我们将默认按钮, 如. OK 按钮连接到 accept() 槽函数; Cancel 按钮连接到 reject() 槽函数. 或者你可以调用 done() 槽函数, 参数是 AcceptedRejected.

另一种方法是先调用 setModal(true) 或 setWindowModality(), 然后再调用 show(). 与 exec() 不同, show() 会立即将控制权返回给调用者. 对于进度条, 调用 setModal(true) 非常有用, 在进度条对话框中, 用户必须与对话框交互, 如. 取消长时间运行操作. 如果你同时使用 show() 和 setModal(true) 执行长操作, 则在处理过程中, 你必须定期调用 QApplication::processEvents(), 使用户能够与对话框交互. (参见 QProgressDialog.)

非模态对话框

非模态 对话框是独立于同一应用程序中的其他窗口, 可以操作的对话框. 文字处理器中的查找和替换对话框通常是非模态的, 允许用户与应用程序的主窗口和对话框交互.

显示非模态对话框调用 show(), 这个函数会立即将控制权返回给调用方.

如果对话框隐藏后, 则调用 show() 会将对话框显示在初始位置. 窗口管理器决定没有被程序员明确放置的窗口的位置. 若想要保留用户移动对话框的位置, 则在 closeEvent() 函数中保存位置, 然后在显示对话框时移动到保存位置.

默认按钮

对话框的 默认 按钮是用户按下 Enter(Return) 键时响应的按钮. 这个按钮指示用户接受对话框设置, 并关闭对话框. 使用 QPushButton::setDefault(), QPushButton::isDefault() 和 QPushButton::autoDefault() 设置和控制对话框的默认按钮.

Esc 键

如果用户在对话框具有焦点时, 按下 Esc 键, 则调用 QDialog::reject(). 这将关闭对话框: 关闭事件 不能被 忽略.

可扩展

可扩展对话框以两种方式显示对话框的功能: 部分对话框(显示常用选项)和完整对话框(显示所有选项). 通常, 一个可扩展的对话框开始显示部分对话框, 并带有 更多 按钮. 用户按下 更多 按钮时, 对话框展开. Extension 示例 展示如何使用 Qt 实现可扩展对话框.

返回值 (模态对话框)

模态对话框经常需要使用返回值, 如. 判断用户按下 确定取消. 对话框调用 accept() 或 reject() 槽函数关闭对话框, exec() 将返回 AcceptedRejected. 如果对话框没有销毁, 你也可以调用 result() 获取返回值.

为了修改对话框的关闭行为, 你可以重新实现函数 accept(), reject() 或 done(). closeEvent() 函数仅用于保存对话框位置或重新实现标准的关闭或取消行为.

代码示例

模态对话框:


  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, Extension Example, Standard Dialogs Example.

成员类型

enum QDialog::DialogCode

The value returned by a modal dialog.

ConstantValue
QDialog::Accepted1
QDialog::Rejected0

属性

This property holds whether show() should pop up the dialog as modal or modeless

By default, this property is false and show() pops up the dialog as modeless. Setting his 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(), and exec().

sizeGripEnabled : bool

This property holds whether the size grip is enabled

A QSizeGrip is placed in the bottom-right corner of the dialog when this property is enabled. By default, the size grip is disabled.

访问函数:

bool isSizeGripEnabled() const
void setSizeGripEnabled(bool)

成员函数

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

Constructs a dialog with parent 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()

Destroys the QDialog, deleting all its children.

[virtual slot] void QDialog::accept()

Hides the modal dialog and sets the result code to Accepted.

参见 reject() and done().

[signal] void QDialog::accepted()

This signal is emitted when the dialog has been accepted either by the user or by calling accept() or done() with the QDialog::Accepted argument.

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

This function was introduced in Qt 4.1.

参见 finished() and 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)

Closes the dialog and sets its result code to 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() signal is emitted.

参见 accept(), reject(), QApplication::activeWindow(), and QCoreApplication::quit().

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

重新实现 QObject::eventFilter().

[virtual slot] int QDialog::exec()

Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.

If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog. If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.

参见 open(), show(), result(), and 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(), or reject().

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

This function was introduced in Qt 4.1.

参见 accepted() and rejected().

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

重新实现 QWidget::keyPressEvent().

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

重新实现 QWidget::minimumSizeHint().

[virtual slot] void QDialog::open()

Shows the dialog as a window modal dialog, returning immediately.

This function was introduced in Qt 4.5.

参见 exec(), show(), result(), and setWindowModality().

[virtual slot] void QDialog::reject()

Hides the modal dialog and sets the result code to Rejected.

参见 accept() and done().

[signal] void QDialog::rejected()

This signal is emitted when the dialog has been rejected either by the user or by calling reject() or done() with the QDialog::Rejected argument.

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

This function was introduced in Qt 4.1.

参见 finished() and accepted().

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

重新实现 QWidget::resizeEvent().

int QDialog::result() const

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

Note: When called on a QMessageBox instance, the returned value is a value of the QMessageBox::StandardButton enum.

Do not call this function if the dialog was constructed with the Qt::WA_DeleteOnClose attribute.

参见 setResult().

void QDialog::setResult(int i)

Sets the modal dialog's result code to i.

Note: 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().