A list view can help you easily display many components dynamically.
Listviews can help you display objects from a model in an attractive way. To use a list view, you have to keep track of three things:
The model, which contains the data you want your list view to display
The delegate, which defines how each element in the model will be displayed
The list view itself, which will display information from the model according to the delegate
If you would like further clarification, the Qt documentation has an informative page on the topic.
Essentials of models and views
A list view has two essential properties we must pay attention to:
model, which accepts the data or the id of the object that holds the data
delegate, which accepts the component we will use to display the data in the model
The model is not visible, as it only contains data. Typically the delegate will be wrapped in a Component so that it is reusable: it serves as a blueprint for how to instantiate each delegate.
Here is an example that contains exactly one list view, one model and one delegate, using a Kirigami.SubtitleDelegate:
importQtQuickimportQtQuick.Controlsas Controlsimportorg.kde.kirigamias KirigamiKirigami.ApplicationWindow {title:"List of Plasma products"width:600height:400pageStack.initialPage:Kirigami.ScrollablePage {ListView {anchors.fill: parentmodel: plasmaProductsModeldelegate: listDelegate }ListModel {id: plasmaProductsModelListElement { product: "Plasma Desktop"; target: "desktop" }ListElement { product: "Plasma Mobile"; target: "mobile" }ListElement { product: "Plasma Bigscreen"; target: "TVs" } }Component {id: listDelegateControls.ItemDelegate {width: ListView.view.widthtext: `${model.product} is KDE software developed for ${model.target} stored at index ${model.index} of this list` } } }}
And the exact same example, inline:
Understanding models
The model contains the data that will be used to populate the list view. Different ways to use models have different ways to access the data:
In the table above, "Qt models" refers to both C++-specific models like QAbstractListModel and QML-specific models like ListModel. This tutorial page will only focus on QML-specific models. Farther ahead we provide a tutorial for Connecting C++ models to QML using QAbstractListModel.
The model.index property is made available to every model and contains the index (the position) of each delegate. It can be shortened to index for convenience.
The model.somerole property mentioned above is just a placeholder, it is not a specific property that comes from QML: somerole can be any role that is defined by the model. In the first code example of this page shown above the table, the plasmaProductsModel model has the product and target roles, which can be accessed with model.product and model.target, respectively.
Just as model.index can be shortened to index, each model.somerole property can be shorted to just somerole (like product) for convenience, but it is recommended that they be turned into required properties:
Additionally, if the model contains only one role or has no role at all, its data can also be accessed with the property model.modelData, which can also be shortened to modelData (and as such would also need to be a required property):
For comparison, here is how the above code would look like with a JavaScript array, with no role:
Using an integer for the model can be useful for very specific cases, namely prototyping and tests:
Understanding views and delegates
Let's go back to the original example:
Unlike the model (which merely contains data) and a delegate Component (which only appears when instantiated), the view is a visual component immediately instantiated and so it needs to either have its dimensions set or use anchors or Layouts.
As views are commonly lists of content the user would want to scroll through, when they are added to a Kirigami.ScrollablePage, views become the main content with little padding around them, and there is no need to make it fill the page. When the view is added to a simple Kirigami.Page, it will require to set its dimensions properly before it will show up. In other words: in the scrollable page above, anchors.fill: parent is not required; if a simple page was used, it would be required.
There are multiple views APIs can be used, some from Qt and some from Kirigami. Here are the most commonly used ones:
The delegate on the other hand always need to have its dimensions set. Generally its dimensions are set to use only the full width of the view.
Common mistakes
The above means that delegates should not have bottom anchors, since the delegate doesn't need to have the same height as the view. In other words, you will probably never want to use anchors.fill: parent.
Additionally, while it is possible to set its dimensions using the parent and anchors, which is usually the view's contentItem, like so:
It is not guaranteed that the delegate's parent will be a view, and so it should be avoided. Instead, use the ListView.view attached property to point to the delegate's parent view:
The most common use of a delegate is within a Component, which does not instantiate the delegate immediately. When a view is constructed, the delegate is then used as a blueprint to make each item in the view.
While you can make your own custom components to be used as delegates without delegate-specific Qt APIs (for example, a Layout containing a few Items), QtQuick Controls does provide delegate APIs that are simpler to use: