1

I was using till now a StackView in QML to go from one screen to the other. But I figured out it is also possible to just create different items that would be set to visible or not visible to update the screen with a new view. I could have for example a header, a main item (Item1) set to visible and a footer. I could then set Item1 to not visible and Item2 to visible.

I was wondering what is the advantage/disadvantage of each solution? (StackView VS views visible/invisible)

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
ArisPlat
  • 13
  • 1
  • 3
  • 4
    The advantage is you have full control of the behavior, the disadvantage is you have to implement the behavior. – dtech Aug 30 '16 at 13:43
  • AFAIK the StackView's main advantage comes in, when you want to have a stack of views, in which you can go back. If your desire is to only have multiple views, that live in parallel, it is probably not what you need. If you have tons of complex, stateless views, it is probably more beneficial to use the loader instead of the 'visible/invisible'-approach. – derM - not here for BOT dreams Aug 31 '16 at 07:07

1 Answers1

4

As @ddriver said, the advantage of using StackView is that you don't have to do it all yourself. I doubt the performance benefit you get from not using StackView (if you get one at all) will outweigh the decline in readability of your code. If I had to maintain your code and saw that you were doing it yourself, the first question I'd ask is why you didn't just use StackView.

  • Transitions: you'd have to maintain animations for each page, which you get for free with StackView - as in, they exist by default and you don't have to write a single line of code to get a nice looking animation.
  • Visibility: you'd probably have to have an index for each page and compare it against a currentIndex property in e.g. your main.qml file. Give each "page" an index and set visible: index == currentIndex for each item. You'd have to ensure that this happens after the animations (if you have any).
  • Memory: The typical use case with StackView is to push Components from which items are instantiated and managed by StackView. If you have a lot of complex pages, this could impact performance if you don't destroy them when they're not visible.

I could have for example a header, a main item (Item1) set to visible and a footer.

Page and ApplicationWindow have this functionality, too.

If you're doing it as a learning exercise, by all means play around with a custom implementation.

If you're aiming for a reliable (StackView is auto-tested and exposed to the public) finished product, use StackView.

Mitch
  • 23,716
  • 9
  • 83
  • 122
  • The problem with Stackview is that if you want to alternate between pages randomly, you can't switch to a page already instantiated easly. If you `push()` pages 1, then 2, then 1 again, you created a new page 1, and all the information stored on the first page 1 will be lost. – Bersan Feb 22 '22 at 13:10
  • @Bersan not if you push an existing `Item`: https://doc.qt.io/qt-6/qml-qtquick-controls2-stackview.html#item-ownership – Mitch Feb 23 '22 at 07:29