1.新建MyDialog.qml
import QtQuick 2.15import QtQuick.Dialogs 1.2Dialog {id: rootvisible: falsetitle: qsTr("弹出对话框")width: 250height: 200}
2.main.qml中调用MyDialog
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15Window {width: 640height: 480visible: truetitle: qsTr("Hello World")MyDialog{id:dialog1visible: falseonVisibilityChanged:{if(!this.visible)result.text = "closed"elseresult.text = "open"}}Button{id:button1text:"Open Dialog"onClicked: {dialog1.open()}}Label{id:resultanchors.top: button1.bottom}}
3.测试结果