代码之家  ›  专栏  ›  技术社区  ›  İbrahim

如何启动QML文件?

  •  0
  • İbrahim  · 技术社区  · 7 年前

    我有4个QML文件: MainMenu.qml , AppArea.qml , Result.qml main.qml .

    当我的应用程序启动时,我希望看到第一页 主菜单。qml公司 全屏显示。有一个按钮(on 主菜单。qml公司 )开始 装置。qml公司 . 当我点击按钮时,我想开始 装置。qml公司 作为全屏新窗口。

    有一个按钮(on 装置。qml公司 ),当我单击该按钮时,我想显示 后果qml公司 但我想看看 后果qml公司 在…上 装置。qml公司 我是说什么时候 后果qml公司 出来吧, 装置。qml公司 不会消失,但 后果qml公司 将出现在 装置。qml公司 .

    上有一个按钮 后果qml公司 . 当我单击按钮时 Repeater 在里面 装置。qml公司 会再生,因为也许 model 属于 中继器 更改方式 1, 2, 3, 4... . 上有一个按钮 装置。qml公司 ,当我单击按钮时,我想打开 主菜单。qml公司 作为全屏新窗口,如 装置。qml公司 .

    实际上,你可以这样想:我的应用程序是这样一个游戏:

    mainmenu.png App_Area.png Resulton_App_Area.png

    我该如何选择这些工作?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mohammad Kanan    7 年前

    除了 mentioned post ,在您的情况下,您正在使用 component 从qml文件,因此您需要加载 组成部分 首先,你的 main.qml 可以是这样的:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.2
    
    Window {
        id: mainWindow
        title: "Main window"
        visible: true
        flags: Qt.Dialog
        modality: Qt.ApplicationModal
        Loader{
            id: mainMenuLoader
        }
            Component.onCompleted: {
                mainMenuLoader.source="mainMenu.qml"
                var mainMenu = mainMenuLoader.item.createObject(mainWindow);
                mainWindow.hide()
        }
    
    }
    

    还有你的 mainMenu.qml 可以如下所示:

    import QtQuick 2.9
    import QtQuick.Window 2.3
    import QtQuick.Controls 2.2
    
    Component {
        id:  mainMenu
        Window {
            id:mmenu
            title: "Main Menu"
            width: 600
            height: 600
            visible: true
            flags: Qt.Dialog
            modality: Qt.ApplicationModal
            Loader{
                id: appAreaLoader
            }
            Text {
                text: "This is mainMenu"
            }
            Button{
                id: loadAppArea
                anchors.centerIn: parent
                text: "Start Game"
                onClicked: {
                    appAreaLoader.source="appArea.qml"
                    var appArea = appAreaLoader.item.createObject(mainMenu);
                    hide()
                }
            }
        }
    }
    

    您需要对连续的窗口执行相同的操作。。。等 而对于 result ,您需要使用 MouseArea :

    装置。qml:

    Component {
        id:  appMenu
        Window {
            id:appMenuWindow
            title: "App Menu"
            width: 600
            height: 600
            visible: true
            flags: Qt.Dialog
            modality: Qt.ApplicationModal
            Loader{
                id:anotherLoader
                visible: true
                anchors.left: appMenuText.left
                anchors.top: appMenuText.bottom
                width: parent.width/3
                height: parent.height/3
            }
            Text {
                id: appMenuText
                text: "This is App Area"
                anchors.centerIn: parent
            }
            Button{
                id: loadResult
                text: "Show Result"
                onClicked: {
                    anotherLoader.source = "result.qml"
                    anotherLoader.visible=true
                }
            }
            Button{
                anchors.right: parent.right
                id: loadMainMenu
                text: "Open main Menu"
                onClicked: {
                    hide()
                    //mmenu.show()
                    anotherLoader.setSource("main.qml")
                }
            }
        }
    }
    

    后果qml:

       Rectangle{
    
            color: "green"
            Text {
                anchors.centerIn: parent
                id: resultxt
                text: qsTr("This is result, Click to close")
            }
            MouseArea {
                 anchors.fill: parent
                 onClicked: { anotherLoader.visible = false
    
                 }
             }
        }