Why this stackview.pop not working?
-
in my program stackview.push() is working but stackview.pop() not working, I tried stackview.pop(null) and stackview.pop(["qrc:/main.qml"]) but still not working
import QtQuick 2.12 import QtQuick.Controls 2.5 import QtQuick.Window 2.0 import QtGraphicalEffects 1.12 import QtQuick.Controls.Universal 2.12 import QtQuick.Layouts 1.12 Page { width: Screen.width height: Screen.height visible: true Rectangle { x: -10 y: 20 width: 1324 anchors.top: parent.top anchors.bottom: parent.bottom anchors.bottomMargin: 70 anchors.topMargin: 0 height: 336 radius: 25 border.width: 5 border.color: "green" Column { anchors.fill: parent anchors.rightMargin: 22 anchors.leftMargin: 22 anchors.topMargin: 14 anchors.bottomMargin: 24 Row { y: 100 height: 65 anchors.left: parent.left anchors.right: parent.right Rectangle { y: 100 anchors.top: parent.top anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter width: 0.95 * parent.width height: 55 radius: 15 border.width: 2 border.color: "red" color: "white" Label { id: label2 x: 195 y: 19 width: 67 height: 27 text: qsTr("Name:") horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.pointSize: 12 } } } Row { anchors.left: parent.left anchors.right: parent.right height: 0.1 * parent.height Rectangle { width: 1262 anchors.top: parent.top anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter height: 0.01 * parent.height Image { id: img } Timer { interval:100; running: true; repeat: true onTriggered: img.source = "image://imgProvider/" + new Date().getTime(); } } } } Button { id: button x: 613 y: 628 text: qsTr("Record") checkable: true font.pointSize: 12 font.family: "Arial" background: Rectangle { id: rect radius: 20 property bool isRed: true color: isRed? "red" : "green" } onClicked:{ rect.isRed = false timer.start() } } Timer{ id: timer interval:15000; running: false; repeat: false onTriggered: img.grabToImage(function(result) { stackView.push( "qrc:/ThirdPage.qml", {"url" : result.url} ) rect.isRed = true }, Qt.size(img.width, img.height)); } Button { id: button1 x: 296 y: 628 width: 100 height: 30 checkable: true text: qsTr("Back") font.pointSize: 12 font.family: "Arial" onClicked: stackView.pop() background: Rectangle { border.color: "gray" color: "white" } } } }
-
@zhmh
a bit clustered, so it fits all in one qml file. I assume your Pages are separated qml files, should work the same way.import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQuick.Window 2.12 import QtQuick.Layouts 1.12 Window { visible: true width: 500 height: 500 title: qsTr("Hello World") StackView{ id:myStackView anchors.fill: parent initialItem: Column{ Button{ width: myStackView.width height: 50 text: qsTr("Page1") onClicked: myStackView.push(page1) } Button{ width: myStackView.width height: 50 text: qsTr("Page2") onClicked: myStackView.push(page2) } } onCurrentItemChanged: if(myStackView.currentItem.popView)myStackView.currentItem.popView.connect(myStackView.pop); } Component{ id: page1 Page{ id:p1 signal popView() onPopView: console.log("button on page 1 clicked") Button { anchors.bottom: parent.bottom text: qsTr("Page 2") onClicked: p1.popView() } } } Component{ id:page2 Page{ id:p2 signal popView() onPopView: console.log("button on page 2 clicked") Button { anchors.bottom: parent.bottom text: qsTr("Page 1") onClicked: p2.popView() } } } }
-
It should work. Are you trying to pop before you push ? Did you confirm that you are inserting the element for sure ?
Please look at the documentation. It has working example. Comparing with the example may help.
-
@J-Hilk in button with id:button1 I used the pop (last lines) and in id: timer I used push()
I want to pop to main.qml which is my first page with ApplicationWindow{}
in main.qml where stackview is defined:main.qml:
property alias stackView: stackView StackView { id: stackView initialItem: main anchors.fill: parent anchors.rightMargin: -28 anchors.bottomMargin: -19 anchors.leftMargin: 28 anchors.topMargin: 19 //some code }
-
@zhmh said in Why this stackview.pop not working?:
property alias stackView: stackView StackView { id: stackView
This is very ugly and should ends with a binding loop issue.
I would change this into:property alias stackView: _stackView StackView { id: _stackView ...
-
@zhmh thats definitely the wrong approach, you shouldn't rely on global/recursive identifier lookup. Thats bound to break really soon.
give your page component pop and push signals, call those from the buttons and connect the signals in the qml file, where your Page and StackWindow instance is.
-
@zhmh
a bit clustered, so it fits all in one qml file. I assume your Pages are separated qml files, should work the same way.import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQuick.Window 2.12 import QtQuick.Layouts 1.12 Window { visible: true width: 500 height: 500 title: qsTr("Hello World") StackView{ id:myStackView anchors.fill: parent initialItem: Column{ Button{ width: myStackView.width height: 50 text: qsTr("Page1") onClicked: myStackView.push(page1) } Button{ width: myStackView.width height: 50 text: qsTr("Page2") onClicked: myStackView.push(page2) } } onCurrentItemChanged: if(myStackView.currentItem.popView)myStackView.currentItem.popView.connect(myStackView.pop); } Component{ id: page1 Page{ id:p1 signal popView() onPopView: console.log("button on page 1 clicked") Button { anchors.bottom: parent.bottom text: qsTr("Page 2") onClicked: p1.popView() } } } Component{ id:page2 Page{ id:p2 signal popView() onPopView: console.log("button on page 2 clicked") Button { anchors.bottom: parent.bottom text: qsTr("Page 1") onClicked: p2.popView() } } } }
-
-
@GrecKo said in Why this stackview.pop not working?:
no, this is fine. https://doc.qt.io/qt-6/qtqml-syntax-objectattributes.html#considerations-for-property-aliases.
Thanks for the update!
Didn't know this.
I am always a little bit paranoiac about possible binding loop issues, they are never easy to find / resolve.