我在QML一个初学者,已经在QTç工作更上StackWidget ++在QML我很困惑使用stackView和已经写下面的代码:如何使用QML StackView?
Window {
visible: true
width: 640
height: 480
title: qsTr("Stack view")
MainForm {
StackView {
id: stackView
x: 0
y: 0
width: 360
height: 360
initialItem: page1
Rectangle {
id: page1
//anchors.fill: parent
color: "lightgreen"
Button {
id: buttonPage1
text: "back to 2"
anchors.centerIn: parent
onClicked: {
stackView.pop() //**Is THIS CORRECT**
stackView.push(page2) //**Is THIS CORRECT**
}
}
TextEdit {
id: te1
width: 105
height: 40
text: "enter"
}
}
Rectangle {
id: page2
//anchors.fill: parent
color: "lightblue"
Button {
id: buttonPage2
text: "back to 1"
anchors.centerIn: parent
onClicked: {
stackView.pop() //**Is THIS CORRECT**
}
}
TextEdit {
id: te2
width: 109
height: 29
text: "enter"
}
}
}
}
}
下面是问题:
在StackWidget中,我使用setCurrentIndex来设置所需的页面,我知道在QML中我应该使用push和pop。在这种情况下,如何使用push和pop根据一些选择在page1和page2之间导航。 ?
最初,我可以加载所有页面到stackView?
当我从stackView弹出一个项目时如何保存页面中的内容?
这听起来像你想[StackLayout](https://doc.qt.io/qt-5/qml-qtquick-layouts-stacklayout.html),而不是StackView。 – jpnurmi