0

In my application I have a normal QML tree with several child nodes below the root element. The application calls for some of the sub trees being visible sometimes and others at other times. One approach I have tried is to setParentItem to null for the sub tree I want hidden (to avoid fireing visibleChange events), saving ptr reference for future connection with root element. Problem I have is that Qt's garbage collector then kicks in and frees all nodes in the sub tree.

Can this be avoided somehow?

dtech
  • 47,916
  • 17
  • 112
  • 190
Johan
  • 1,633
  • 15
  • 22

1 Answers1

2

Objects without parents and references to them as candidates for collection. On top of that, QMLs garbage collection is actually broken. You can disable QML garbage collection altogether for an object by overriding ownership to C++ as described here.

There might be more ways to solve this depending on your actual usage scenarios, for example a StackView.

Community
  • 1
  • 1
dtech
  • 47,916
  • 17
  • 112
  • 190
  • Unfortunately, toggling the visible flag fires onVisibleChange events which I don't want. Will check if I can disable tomorrow. Thanks – Johan Dec 06 '16 at 18:31
  • @Johan - you can try setting opacity to 0 to hide and enabled to false to disable input. – dtech Dec 06 '16 at 18:56
  • That's my current approach, and it works fine. But seems so much cleaner to remove the whole tree (and faster than recursively set opacity) . I don't think the other solution you proposed will work for me as it requires too much re-write, it's a huge codebase, but it is a neat solution and I'll have that in mind for future applications – Johan Dec 06 '16 at 19:28