主窗口qml文件
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
Window {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
M_Dialog {
id: m_dialog
}
Button {
id: btn_dialog
width: 200
height: 150
anchors.centerIn: parent
Text {
anchors.centerIn: parent
text: "打开消息框"
font.pixelSize: 20
}
onClicked: {
m_dialog.show()
mainWindow.hide()
}
}
Connections {
target: m_dialog
function onBack() {
mainWindow.show()
m_dialog.hide()
}
}
Component.onDestruction: console.log("over")
}
子窗口A
import QtQuick 2.0
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
Window {
id: dialog
width: 500
height: 300
title: "MyDialog"
signal back()
M_MessageBox {
id: m_messageBox
}
Button {
id: btn_close
width: 80
height: 40
anchors.centerIn: parent
Text {
anchors.centerIn: parent
font.pixelSize: 20
text: "退回"
}
onClicked: {
m_messageBox.show()
}
}
Connections {
target: m_messageBox.btn_ensure
function onEnsured() {
m_messageBox.hide()
back()
}
}
Connections {
target: m_messageBox.btn_exit
function onExited() {
m_messageBox.hide()//用hide不要用close,用close直接全关
}
}
}
子窗口B
import QtQuick 2.0
import QtQuick.Window 2.14
import QtQuick.Controls 2.0
Window {
id: messageBox
width: 250
height: 100
modality: Qt.WindowModal
property alias btn_ensure: btn_ensure
property alias btn_exit: btn_exit
Button {
id: btn_ensure
width: 100
height: 50
anchors.left: parent.left
anchors.leftMargin: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
Text {
anchors.centerIn: parent
text: "ensure"
}
signal ensured()
onClicked: {
console.log("MessageBox ensure")
ensured()
}
}
Button {
id: btn_exit
width: 80
height: 50
anchors.right: parent.right
anchors.rightMargin: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
Text {
anchors.centerIn: parent
text: "exit"
}
signal exited()
onClicked: {
console.log("MessageBox exit")
exited()
}
}
onClosing: function(closeevent){
//CloseEvent的accepted设置为false就能忽略该事件
closeevent.accepted = false
this.hide()
}
}