QtQuickControls2 对话框在外部单击时消失
QtQuickControls2 Dialog vanishing on click outside
我正在尝试使用 QtQuickControls2 对话框:
Dialog {
id: dialog
x: parent.width/2-width/2
y: parent.height/2-height/2
width:300
height:200
title: "Warning"
modal: true
standardButtons: Dialog.Ok
visible: false
onAccepted: console.log("Ok clicked")
}
Button {
id: button
objectName: "doSomethingButton"
onClicked: {
if(problemFlag==true)
dialog.visible=true
}
}
如果按钮被点击并且problemFlag
是true
应该触发。我读到如果 modal
设置为 true
则用户无法与程序的其余部分进行交互。但是,如果我单击对话框外的某处,它就会消失(无需单击“确定”)。
我运行也喜欢这个。您需要设置 closePolicy
以便它仅在按下转义键时关闭:
closePolicy: Popup.CloseOnEscape
docs提到这个:
This property holds whether the popup is modal.
Modal popups often have a distinctive background dimming effect
defined in overlay.modal, and do not allow press or release events
through to items beneath them.
On desktop platforms, it is common for modal popups to be closed only
when the escape key is pressed. To achieve this behavior, set
closePolicy to Popup.CloseOnEscape.
The default value is false.
很难看出区别,但它确实存在:模态弹出窗口不允许按下或释放事件传递到它们下面的项目,但这并不意味着它们不会 关闭.
我不记得这背后的原因,但如果我不得不猜测的话,我会说这与 Qt Quick Controls 2 首先是为移动设备构建的事实有关。在移动设备上,您通常:
- 想要弹出窗口的调光效果。
- 不希望在其外部发生的触摸事件传递到其下方的项目。
- 希望在用户点击弹出窗口外部时关闭弹出窗口。
如果你看一下小部件,QDialog::modal
的文档说:
Setting this property to true is equivalent to setting
QWidget::windowModality to Qt::ApplicationModal.
如果你再看看 Qt::WindowModality
:
This enum specifies the behavior of a modal window. A modal window is one that blocks input to other windows. [...]
和:
The window is modal to the application and blocks input to all windows.
因此,尽管模态 QDialog
在外部发生点击时不会关闭,但不让事件通过和不关闭之间的区别并不是一个新的区别。
我正在尝试使用 QtQuickControls2 对话框:
Dialog {
id: dialog
x: parent.width/2-width/2
y: parent.height/2-height/2
width:300
height:200
title: "Warning"
modal: true
standardButtons: Dialog.Ok
visible: false
onAccepted: console.log("Ok clicked")
}
Button {
id: button
objectName: "doSomethingButton"
onClicked: {
if(problemFlag==true)
dialog.visible=true
}
}
如果按钮被点击并且problemFlag
是true
应该触发。我读到如果 modal
设置为 true
则用户无法与程序的其余部分进行交互。但是,如果我单击对话框外的某处,它就会消失(无需单击“确定”)。
我运行也喜欢这个。您需要设置 closePolicy
以便它仅在按下转义键时关闭:
closePolicy: Popup.CloseOnEscape
docs提到这个:
This property holds whether the popup is modal.
Modal popups often have a distinctive background dimming effect defined in overlay.modal, and do not allow press or release events through to items beneath them.
On desktop platforms, it is common for modal popups to be closed only when the escape key is pressed. To achieve this behavior, set closePolicy to Popup.CloseOnEscape.
The default value is false.
很难看出区别,但它确实存在:模态弹出窗口不允许按下或释放事件传递到它们下面的项目,但这并不意味着它们不会 关闭.
我不记得这背后的原因,但如果我不得不猜测的话,我会说这与 Qt Quick Controls 2 首先是为移动设备构建的事实有关。在移动设备上,您通常:
- 想要弹出窗口的调光效果。
- 不希望在其外部发生的触摸事件传递到其下方的项目。
- 希望在用户点击弹出窗口外部时关闭弹出窗口。
如果你看一下小部件,QDialog::modal
的文档说:
Setting this property to true is equivalent to setting QWidget::windowModality to Qt::ApplicationModal.
如果你再看看 Qt::WindowModality
:
This enum specifies the behavior of a modal window. A modal window is one that blocks input to other windows. [...]
和:
The window is modal to the application and blocks input to all windows.
因此,尽管模态 QDialog
在外部发生点击时不会关闭,但不让事件通过和不关闭之间的区别并不是一个新的区别。