我在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