StackView has screen 1 as initial item. Screen 2 is pushed. Screen 2 is now popped.
Screen 2 gets destroyed after screen 1 is made visible.
Is this expected behavior? Why does it work this way?
I expected the popped screen to get destroyed before the previous screen is made visible.
Code to demonstrate the same:
StackView {
id: stack
initialItem: mainView1
anchors.fill: parent
}
Component {
id: mainView1
Text {
text: "1"
onVisibleChanged: console.log(text, visible)
Component.onCompleted: console.log(text, "completed")
Component.onDestruction: console.log(text, "destroyed")
}
}
Component {
id: mainView2
Text {
text: "2"
onVisibleChanged: console.log(text, visible)
Component.onCompleted: console.log(text, "completed")
Component.onDestruction: console.log(text, "destroyed")
}
}
Console output:
qml: 1 completed
qml: Push
qml: 2 completed
qml: 1 false
qml: Pop
qml: 1 true
qml: 2 false
qml: 2 destroyed
Motivation behind the question:
I need to control a C++ timer from QML. Timer needs to be started when widget is created/visible and stopped when widget is destroyed/hidden.
The above depicted behavior of StackView prevents me from achieving it.
When I pop screen 2, screen 1 is first made visible, so my timer starts. Screen 2 then gets destroyed, as a result, my timer is stopped.