QDialog:显示()与打开()

QDialog: show() vs open()

QDialog::show() and QDialog::open()有什么区别?

你自己链接到文档很有趣。

QDialog::open 将 window 呈现为模态。 QWidget::show 是所有 QWidget 实现的基本方法,可确保向用户显示 window。

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

show() 将只显示对话框而不影响程序中的其他 windows。 open()show() window + 阻止其他 windows 通过 setWindowModality() 访问,即,它变成模态 window.

例如,如果您想要打开一个文件,并且您不希望用户在选择一个文件并关闭该对话框之前能够在程序中执行任何操作,这将很有用。

引用自Qt's manual

A modal 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 be application modal (the default) or window 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 its exec() function. When the user closes the dialog, exec() will provide a useful return value. 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 Accepted or Rejected.

正如文档中所述,QDialog::open()

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

而 QDialog::show(),实际上是 QWidget::show(),只会将您的对话框显示为标准的非模态小部件。

鉴于对话框的模式是通过执行 show() 或 open() 来控制的,QDialog::modal() 属性似乎是 superfluous/unused。 还是调用 show() 或 open() 会相应地动态设置此 bool 属性?