qt - How to Use QML StackView? -
i beginner in qml , have worked more on stackwidget in qt c++.in qml confused use stackview , have written following code:
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 2" anchors.centerin: parent onclicked: { stackview.pop() //**is correct** stackview.push(page2) //**is correct** } } textedit { id: te1 width: 105 height: 40 text: "enter" } } rectangle { id: page2 //anchors.fill: parent color: "lightblue" button { id: buttonpage2 text: "back 1" anchors.centerin: parent onclicked: { stackview.pop() //**is correct** } } textedit { id: te2 width: 109 height: 29 text: "enter" } } } } }
below questions:
in stackwidget using setcurrentindex set desired page , know in qml should use push , pop. in case how use push , pop navigate between page1 , page2 based on selection. ?
initially, can load pages stackview?
how save content in page when pop item stackview?
i know not answer question on how use stackview
, because think don't want have stackview
following description.
the use-case of stackview
is, when have pages - names suggests - on stack. if want switch between pages, not determinable, 1 logically below another, stackview
not want, , might want consider swipeview
.
in swipeview
pages coexist in side-by-side manner. since qt 5.9 have interactive
property might disable swipe behaviour. here can choose page want show setting currentindex
.
however, swipeview
create pages needed, reduce memory , cpu load (effectively disabling bindings of unloaded pages). might result in data loss, if data not stored in model
outside page itself.
if want have pages loaded @ same time, , want switch visible one, might go simple custom component:
item { property int currentindex page1 { visible: parent.currentindex === 0 } page2 { visible: parent.currentindex === 1 } page3 { visible: parent.currentindex === 2 } ... }
or go like:
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 { } }
Comments
Post a Comment