【发布时间】:2020-12-19 22:39:04
【问题描述】:
我有一个窗口,其中包含一个 Rectangle 和一个按钮,Loader 在该按钮上将 AnotherClass.qml 加载到 Rectangle 中。这按预期工作,但是当从 AnotherClass.qml 中按下另一个按钮时,会出现两个带有 MainMenu.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}
}
}
另一个类.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"
}
} }
【问题讨论】: