The ListModel is a simple container of ListElement definitions, each containing data roles. The contents can be defined dynamically, or explicitly in QML.
The number of elements in the model can be obtained from its count property. A number of familiar methods are also provided to manipulate the contents of the model, including append(), insert(), move(), remove() and set(). These methods accept dictionaries as their arguments; these are translated to ListElement objects by the model.
Elements can be manipulated via the model using the setProperty() method, which allows the roles of the specified element to be set and changed.
Today in this tutorial we are learn Qt QML model using QML ListModel and view we used in this tutorial is ListView.
First step is to create QtQuick Application Project and Create .qml files MyListModel.qml and MyListDelegate.qml files.
Add the content to the MyListModel.qml
import QtQuick 2.15import QtQuick.Controls 2.5ListModel{ListElement{ about:"AbstractButton" description:"Abstract base type providing functionality common to buttons " }ListElement{ about:"Button" description:"Push-button that can be clicked to perform a command or to answer a question " }ListElement{ about:"CheckBox" description:"Check button that can be toggled on or off " }ListElement{ about:"DelayButton" description:"Check button that triggers when held down long enough " }ListElement{ about:"RadioButton" description:" Exclusive radio button that can be toggled on or off " }ListElement{ about:"RoundButton" description:" A push-button control with rounded corners that can be clicked by the user" }ListElement{ about:"Switch" description:"Button that can be toggled on or off " }ListElement{ about:"ToolButton" description:"Button with a look suitable for a ToolBar " }}
and add below content to the MyListDelegate.qml file :
So we are creating two files one is MyListDelegate.qml and MyListModel.qml ,Letβs use this model file and delegate file inside the listview in main file ,you can use anywhere you want to use in listview.
In main.qml file I used extra component for adding the element . So whenever user press the plus button dialog will open and user can add list element .
So Now you can build the project and see the output :