QML中的StackView替代 [英] StackView alternative in QML
问题描述
到目前为止,我一直使用QML中的StackView从一个屏幕转到另一个屏幕.但是我发现也可以只创建不同的项目,将其设置为可见或不可见,以使用新视图更新屏幕.例如,我可以有一个页眉,一个设置为可见的主项目(Item1)和一个页脚.然后,我可以将Item1设置为不可见,将Item2设置为可见.
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.
我想知道每种解决方案的优点/缺点是什么? (StackView VS视图可见/不可见)
I was wondering what is the advantage/disadvantage of each solution? (StackView VS views visible/invisible)
推荐答案
正如@ddriver所说,使用StackView
的优点是您不必自己做所有事情.我怀疑从不使用StackView所获得的性能收益(如果您一无所获)是否会超过代码可读性的下降.如果我必须维护您的代码并看到您自己在做,那么我要问的第一个问题是为什么您不只是使用StackView
.
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
.
- 过渡:您必须维护每个页面的动画,您可以通过
StackView
免费获得这些动画-例如,默认情况下它们存在,并且您无需编写任何代码即可获得不错的效果动画. - 可见性:您可能必须为每个页面都有一个索引,并将其与例如中的
currentIndex
属性进行比较.您的main.qml
文件.给每个页面"一个索引,并为每个项目设置visible: index == currentIndex
.您必须确保在动画之后(如果有)在之后发生这种情况. - 内存:
StackView
的典型用例是推送Component
,从中实例化和管理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.
例如,我可以有一个页眉,一个设置为可见的主项目(Item1)和一个页脚.
I could have for example a header, a main item (Item1) set to visible and a footer.
Page
和 ApplicationWindow
也具有此功能.
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.
如果您要获得可靠的(StackView
经过自动测试并向公众展示)成品,请使用StackView
.
If you're aiming for a reliable (StackView
is auto-tested and exposed to the public) finished product, use StackView
.
这篇关于QML中的StackView替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!