Dynamically add tabs - declarative approach?
-
I need to extend a tab view that currently has a fixed number of tabs. It is implemented using
TabBar
andStackLayout
. The buttons and stack items are just coded directly as nested QML items. These existing tabs all show very different things (e.g. a list view in one, a chart in another, etc.).For the new behaviour, new tabs can appear dynamically at certain times. The new tabs are of a homogenous type (essentially they represent a collection of text logs), but how many there are, and details such as their labelling, are not known until run time.
I have been able to get this to work using an imperative approach. I have written a JS function that in response to relevant events dynamically creates the new buttons and the new stacked items and adds them to the respective containers. However, I am wondering if there is a cleaner way to do this more declaratively.
I wondered if a
Repeater
together with some model might work. I haven't had cause to useRepeater
before so have no experience with it. Reading the document, I am somewhat put off by this statement: "The Repeater type creates all of its delegate items when the repeater is first created." Does this mean that it cannot dynamically adjust itself to a changing model? That would seem to limit its applicability, but it is possible that I have misunderstood what it means.Any thoughts on using
Repeater
, or any other suggestions, would be welcomed. Thanks. -
The Repeater is the right answer. The Repeater can change and grow as things are added to the model.
You can test this out easily by using a ListModel in your QML code and programmatically adding and removing entries to the model. If you want help then create a barebones app in a test project and put together just the code for this. Then share that main.qml (and other files) here. We can help you test this out. -
The Repeater is the right answer. The Repeater can change and grow as things are added to the model.
You can test this out easily by using a ListModel in your QML code and programmatically adding and removing entries to the model. If you want help then create a barebones app in a test project and put together just the code for this. Then share that main.qml (and other files) here. We can help you test this out. -
Models in QML depend upon notification.
This will update when changing things in the model because it has notification signals:
ListModel { id: lmodel } Repeater { model:lmodel }
This will not if you try and modify vmodel elements (unless the change causes the vmodel variable itself to change):
property var vmodel: [1,2,3] Repeater { model: vmodel }
So code like vmodel.push(4) won't provide signals to change for the repeater to see.