问题
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. yourmain.qml
file. Give each "page" an index and setvisible: 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 pushComponent
s from which items are instantiated and managed byStackView
. 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