简体   繁体   English

StackView - 前一个屏幕可见后弹出的屏幕被破坏

[英]StackView - Popped screen gets destroyed after previous screen is made visible

StackView has screen 1 as initial item. StackView 将屏幕 1 作为初始项目。 Screen 2 is pushed.屏幕 2 被按下。 Screen 2 is now popped.屏幕 2 现在弹出。
Screen 2 gets destroyed after screen 1 is made visible.屏幕 1 可见后屏幕 2 被破坏。
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:控制台 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.我需要从 QML 控制一个 C++ 定时器。 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. StackView 的上述行为阻止了我实现它。
When I pop screen 2, screen 1 is first made visible, so my timer starts.当我弹出屏幕 2 时,屏幕 1 首先是可见的,所以我的计时器开始了。 Screen 2 then gets destroyed, as a result, my timer is stopped.然后屏幕 2 被破坏,结果,我的计时器停止了。

The reason why this occurs is due to StackView animation.出现这种情况的原因是由于 StackView animation。 Both views must exist during the default push and pop animations as they both appear on screen at the same time.在默认推送和弹出动画期间,这两个视图都必须存在,因为它们都同时出现在屏幕上。

You likely can solve your problem using the StackView attached signals:您可能可以使用 StackView 附加信号解决您的问题:

https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#attached-signals https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#attached-signals

These are signals that fire on your views regarding their individual status as the StackView pushes and pops.当 StackView 推送和弹出时,这些信号会在您的视图上触发有关其个人状态的信号。

If you haven't used attached signals before, here's some documentation on how they work:如果您以前没有使用过附加信号,这里有一些关于它们如何工作的文档:

https://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#attached-properties-and-attached-signal-handlers https://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#attached-properties-and-attached-signal-handlers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM