如何使用QML StackView? 我是QMl的初学者,在QtC++中对STACKWIDGET有了更多的研究。在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" } } } } }
以下是问题:如何使用QML StackView? 我是QMl的初学者,在QtC++中对STACKWIDGET有了更多的研究。在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" } } } } },qt,qml,qtquick2,qt-quick,qtquickcontrols,Qt,Qml,Qtquick2,Qt Quick,Qtquickcontrols,以下是问题: 在StackWidget中,我使用setCurrentIndex来设置所需的页面,我知道在QML中,我应该使用push和pop。在这种情况下,如何根据一些选择使用推送和弹出在page1和page2之间导航 最初,我可以将所有页面加载到堆栈视图中吗 当我从stackView弹出一个项目时,如何保存页面中的内容 我知道我不会确切回答您关于如何使用StackView的问题,这是因为我认为您不想让StackView跟随您的描述 StackView的用例是,当页面(如名称所示)位于堆栈上时。
我知道我不会确切回答您关于如何使用
StackView
的问题,这是因为我认为您不想让StackView
跟随您的描述
StackView
的用例是,当页面(如名称所示)位于堆栈上时。如果您只想在页面之间切换,在不可确定的页面上,哪个逻辑在另一个页面下面,“代码”> StaveVIEW/COD>不是您想要的,您可能需要考虑<代码> SIPPEVIEW
在SwipeView
中,页面以并排方式共存。自Qt5.9以来,它们有一个交互式
属性,您可以使用该属性禁用刷卡行为。
在这里,您可以通过设置currentIndex
来选择要显示的页面
但是,SwipeView
将根据需要创建其页面,以减少内存和CPU负载(有效地禁用已卸载页面的绑定)。如果数据未存储在页面本身外部的模型中,这可能会导致数据丢失
如果希望同时加载所有页面,并且只希望切换可见页面,则可以使用一个简单的自定义组件:
Item {
property int currentIndex
Page1 { visible: parent.currentIndex === 0 }
Page2 { visible: parent.currentIndex === 1 }
Page3 { visible: parent.currentIndex === 2 }
...
}
或者你会说:
MyView.qml
Item {
id: root
property int currentIndex: 0
default property Item newContent
onNewContentChanged: {
newContent.parent = root
newContent.visible = Qt.binding(bindingsClosure(root.children.length - 1))
}
function bindingsClosure(index) { return function() { return root.currentIndex === index } }
}
main.qml
MyView {
Page1 { }
Page2 { }
Page3 { }
}
您上面的答案几乎符合我的要求,我将尝试swipeView,然后将其标记为有答案。听起来您想要的是代替StackView。