Solved When to use StackView
-
Hi,
This might be a beginner question, but I am not really sure when it is appropriate to use a
StackView
. Right now, I have it like this:Item { id: mainWindow Screen1 { id: screen1 } Screen2 { id: screen2 } Screen3 { id: screen3 } }
And when I want to navigate between my items, I set their visible properties accordingly. I wonder what the advantage of
StackView
is. Are the items created dynamically in aStackView
? Does it offer other advantages? -
The more screens you add, the harder it is to maintain visibility setting.
With StackView you get:
- attached properties
- transition effects
- ease of adding / removing screens
- ability to search through the stack
- no need to worry about background screens showing up behind current one
- ability to manage the memory better (pages are may stay uninitialized until they are shown)
But if manually setting visibility is enough for your case, then it's a perfectly good solution.
-
Thank you for the answer. As my application might get too big to initialize everything at startup, I thought it might make sense to dynamically create them. In a StackView, if I push a new screen, is it then created dynamically?
-
@maxwell31 hi,
Doc :If the only argument needed is "item", the following short-hand notation can be applied: stackView.push(yourItem) You can push several items in one go by using an array of property lists. This is more efficient than pushing items one by one, as StackView can then load only the last item in the list. The rest will be loaded as they are about to become the current item (which happens when the stack is popped). The following example shows how to push an array of items: stackView.push([{item: yourItem1}, {item: yourItem2}])
-
@maxwell31 said in When to use StackView:
In a StackView, if I push a new screen, is it then created dynamically?
Yes, see @LeLev 's answer.
As the documentation suggests, it's good to wrap such items in
Component
, then they are parsed at startup (so you get QML errors early), but not loaded until you push them to the stack view.If you need to do it even more dynamically, you can push QML file name or use Qt.createComponent(). At least I think it should also work ;-) But then the QML is parsed during push - which is slightly worse when it comes to error reporting.