2

I have application which uses StackView and pushes a lot of dynamic created objects into this StackView and I need some way to destroy this objecst when they are popped from StackView. If I use Controls 1 Stackview, I can just push object with destroyOnPop property (like it was shown in this question):

tablesStack.push({item: view, destroyOnPop: true})

but it doesn't applicable to Controls 2 StackView. What is correct solution of this problem?

I know only one way: call object.destroy() with delay when I pop it, but this way looks like kludge. If it is important, I can add any code to dynamic generated objects

Community
  • 1
  • 1
aknew
  • 1,101
  • 7
  • 23

1 Answers1

3

Controls 2 StackView has simplified ownership semantics. StackView takes ownership of any item that it has dynamically instantiated. If you have created the instance, then you're in control of the instance. In other words, if you want StackView to destroy, push Components or URLs and let StackView create the instances.

If you need access the created instance, you can simply handle the return value:

var view = stackView.push(component, {"foo": 1, "bar": 2})
view.doSomething()
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • It works with Qt.createComponent from file, thanks, but I haven't understand how I should change my code when I use Qt.createQmlObject ? My test code is here https://gist.github.com/anonymous/372c58e7f53531bd6e51950d611e33a7 – aknew Aug 21 '16 at 14:30
  • Out of curiosity, what is the main reason for using `Qt.createQmlObject()`? It is a nice trick for avoiding hard dependencies etc. but in most cases more explicit components are seemingly better. Anyway, with QtQuick Controls 2.1 in Qt 5.8 you can do: `Rectangle { StackView.onDeactivated: destroy() }`. With 2.0 you you'll presumably have to resort to checking the status: `Rectangle { StackView.onStatusChanged: if (StackView.status === StackView.Inactive) destroy() }` – jpnurmi Aug 21 '16 at 14:49
  • It works, thanks. Reason to use createQmlObject is very simple: I dynamic create views for tables from few sqlite bases and I don't know what fields will contain tables in runtime. Historicaly I often changed tables struct in the beginning and it was just way to not forget change some qml. If you are interested in my code, here is my github https://github.com/aknew/CoinsVariationBase – aknew Aug 21 '16 at 15:44
  • I have been wrong, StackView.onStatusChanged: is not correct because it triggers on both pop this item or push new, so if I push item whis this code, push another item after it and tried to pop - I cannot go back – aknew Aug 21 '16 at 16:39
  • 2
    I have created [QTBUG-55405](https://bugreports.qt.io/browse/QTBUG-55405) to make sure that this oversight gets fixed in the future releases. But for now, we need a temporary workaround for you, right? I don't have access to a development environment right now so I cannot play with it, but checking `StackView.index` to see whether the item is on the stack could be one possibility. It gets ugly and clumsy, but basically something like `StackView.onIndexChanged: { if (StackView.index === -1 && StackView.status === StackView.Inactive) destroy() }` hopefully does the trick. – jpnurmi Aug 21 '16 at 17:44
  • The last solution really works, but you are right - it is strange, so I will wait Controls 2.1. But thanks anyway for you answers and time. – aknew Aug 21 '16 at 18:19