Mark Mark - 11 days ago 13
Javascript Question

QML: StackView and replace

I'm not sure to use

StackView
correctly.
I have several QML objects and I show them replacing the currentItem in the
StackView
. This is an easy way to have transitions without use two
Loaders
.

Something like:

import QtQuick 2.5
import QtQuick.Controls 1.4

StackView {
id: stack
anchors.fill: parent
initialItem: initObj
}

Component {
id: initObj
ModuleHome {}
}

Component {
id: obj1
Module1 {}
}

// ...

stack.push({item: item, replace: true, destroyOnPop: true})


It happened I had a bug in one page and I noticed the error messages in the debug console were repeated as many times as I pushed the object.

I.e.
First time:


qrc:/MyObject.qml:15: TypeError: Cannot write property of null


Second time:


qrc:/MyObject.qml:15: TypeError: Cannot write property of null

qrc:/MyObject.qml:15: TypeError: Cannot write property of null


Third time:


qrc:/MyObject.qml:15: TypeError: Cannot write property of null

qrc:/MyObject.qml:15: TypeError: Cannot write property of null

qrc:/MyObject.qml:15: TypeError: Cannot write property of null


I'm wondering if the
replace
flag in the
push
method does actually
pop
(and therefore destroy) the object and all objects contained.

Answer

http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html

There are three primary navigation operations in StackView: push(), pop(), and replace (replace by specifying argument replace to push()). These correspond to classic stack operations where "push" adds an item to the top of a stack, "pop" removes the top item from the stack, and "replace" is like a pop followed by a push, in that it replaces the topmost item on the stack with a new item

I think your problem might be related to this:

QML StackView: Item Destroyed on pop

(use Item instead of Component)

Comments