Qt 在一个窗口QML中显示多个类

Qt 在一个窗口QML中显示多个类,qt,qml,Qt,Qml,我有一个窗口,其中包含一个矩形和一个按钮,加载程序在该按钮上将另一个class.qml加载到该矩形中。这可以按预期工作,但当从另一个class.qml中按下另一个按钮时,会出现两个带有main menu.qml的窗口 MainMenu.qml: Window { id: window visible: true width: 640 height: 480 title: "MainMenu.qml" Rectangle

我有一个窗口,其中包含一个矩形和一个按钮,加载程序在该按钮上将另一个class.qml加载到该矩形中。这可以按预期工作,但当从另一个class.qml中按下另一个按钮时,会出现两个带有main menu.qml的窗口

MainMenu.qml:

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: "MainMenu.qml"
    
    Rectangle {
        id: rect
        anchors.fill: window

        Button {
            id: calcButton
            height: 100
            anchors.horizontalCenterOffset: 320
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 220

            onClicked: {
                pageLoader.source = "AnotherClass.qml"
            }

        }
        Loader { id: pageLoader; sourceComponent: rect}  
}
}
另一个class.qml:

Rectangle {
    id: window
    visible: true
    width: 640
    height: 480

     Button {
            id: calcButton
            height: 100
            anchors.horizontalCenterOffset: 320
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 220

            onClicked: {
                pageLoader.source = "MainMenu.qml"
            }
        }
        Loader { id: pageLoader; sourceComponent: rect} }
我决定尝试以不同的方式实现它,但问题仍然是一样的

Rectangle {
    id: window
    visible: true
    width: 640
    height: 480

    MainMenu{
        id: mm
    } 

    Button {
            id: calcButton
            height: 100
            anchors.horizontalCenterOffset: 320
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 220

            onClicked: {
                mm.pageLoader.source = "MainMenu.qml"
            }
        } }

实际上,您的代码有很多问题。您可能需要多读一点关于如何使用Loader的书。之所以有两个单独的窗口,是因为您使用加载程序创建了MainMenu的第二个实例。实现所需功能的一种方法是使用StackView

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: "MainMenu.qml"

    StackView {
        id: stack
        initialItem: mainMenu
        anchors.fill: parent
    }

    Component {
        id: mainMenu

        Rectangle {
            id: rect

            Button {
                id: calcButton
                height: 100
                anchors.horizontalCenterOffset: 320
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.top
                anchors.topMargin: 220

                onClicked: {
                    stack.push(anotherClass)
                }
            }
        }
    }

    Component {
        id: anotherClass

        Rectangle {

            Button {
                id: calcButton
                height: 100
                anchors.horizontalCenterOffset: 320
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.top
                anchors.topMargin: 220

                onClicked: {
                    stack.pop()
                }
            }
        }
    }
}