QML如何点击按钮弹出新的qml窗口

主窗口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()
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值