StackView alternative in QML

匆匆过客 提交于 2019-12-23 02:34:40

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/39227223/stackview-alternative-in-qml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!