πŸ’»
Programming Concept
  • πŸ‘¨β€πŸ”¬About Me
  • πŸ“—C++ STL Concept 2023
    • πŸ“–STL in CPP ( Part I)
    • πŸ“–STL in CPP ( Part II)
    • πŸ“–STL in CPP ( Part III )
    • πŸ“–STL in CPP ( Part IV )
    • πŸ“–STL in CPP ( Part V )
    • πŸ“–STL in CPP ( Part VI )
    • πŸ“–STL in CPP ( Part VII )
  • ✏️Pointers in C / C++
    • πŸ“–Pointers in details
  • πŸ“˜Strings in CPP 2023
  • πŸ•žThread in C++ 2023
  • πŸ““Smart Pointer in C++ 2023
  • Basic C++ Topics
    • πŸ’ΌType Casting in C++ 2023
  • πŸ’»Advanced C++ Programming
    • πŸŽ“Topics (Syllabus)
    • πŸ§‘β€πŸŽ“Inheritance and Polymorphism
    • πŸ–ŠοΈException Handling
    • πŸƒRuntime Type Information
    • πŸ“”An Overview of Templates
    • πŸ“‘Template in C++
  • πŸ’»Embedded Linux
    • πŸ–₯️Embedded Linux OS
      • Build Yocto Project Step By Step
      • How to install apt-get to the Yocto Project image
      • Installing gcc/g++ toolchain in yocto project
      • Installing GDB in yocto project
      • Installing ssh-server-dropbear for embedded linux in yocto project
      • Add Python To A Build
      • Add Boost To A Build
      • Adding Valgrind To Build
      • How To Add A Custom App To A Yocto Build
      • Raspberry Pi
    • πŸ“ΆCommunication Protocols
      • ✏️Working With I2C using c++
      • πŸ“”SPI Implementation with source code in C++
      • πŸ““Linux Serial Ports Using C/C++
      • πŸ–±οΈUART
      • πŸ–¨οΈUniversal GPIO Access
  • πŸ§‘β€πŸ’»QT QML
    • πŸ“˜QT QML Concept 2023
      • Why Qt Framework and QML
      • Sqlite3 for qtquick application
      • How To Install sqlite3 in windows 11 || Windows 10
      • Signals and Slots in Qt QML
      • Working with Signals and Slots in QML
      • Sorting QML ListModels
      • ListView In QML With Section Delegate
      • Registration of custom enum in QML
      • Registering a QML Type as a Singleton object
      • Using enumerations in QML without C ++
      • Registering a Singleton object to use β€œStatic” methods in QML
      • Connecting JavaScript files to other JavaScript files in a Qt / QML project
      • Positioning in Qt QML with anchors
      • TextInput validators in Qt Qml
      • Qt Qml Abstract Item Model using C++
      • Qt QML List Model
      • How to Register your C++ Class as a QML Type
      • Jabra Speaker Connect Qt Project
      • Qt | One article summarizing QObject
  • ▢️QT Concept
    • ℹ️Icon button
    • πŸ’»Register QML Singletone in CP
Powered by GitBook
On this page

Was this helpful?

  1. QT QML
  2. QT QML Concept 2023

Qt QML List Model

PreviousQt Qml Abstract Item Model using C++NextHow to Register your C++ Class as a QML Type

Last updated 1 year ago

Was this helpful?

The ListModel is a simple container of 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 property. A number of familiar methods are also provided to manipulate the contents of the model, including (), (), (), () and (). These methods accept dictionaries as their arguments; these are translated to objects by the model.

Elements can be manipulated via the model using the () 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.15
import QtQuick.Controls 2.5
ListModel{
    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 :

import QtQuick 2.15
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.3
ItemDelegate{
    width: root.width
    contentItem: ColumnLayout{
        RowLayout{
            Layout.preferredWidth: parent.width
            ColumnLayout{
                Layout.fillWidth: true
                spacing: 5
                Label{
                    text: about
                    font.pixelSize: 24
                    Layout.leftMargin: 5

                }
                Label{
                    text: description
                    font.pixelSize: 14
                    Layout.leftMargin: 5
                }
            }
            Button{
                text: qsTr("Learn More")
                highlighted: true
                Layout.alignment: Qt.AlignRight
                onClicked: {

                }
            }
        }
    }

}

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.

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.3
ApplicationWindow {
    id:root
    width: 640
    height: 480
    visible: true
    title: qsTr("Qt QML List Model")

    Dialog{
        id:addDialog
        width: 400
        height: root.height*0.8
        x:(root.width - addDialog.width)/2
        y:(root.height - addDialog.height)/2
        title: qsTr("Add Control Item")
        function checkCheked(index){
            if(index===1){
                mainButton.text = qsTr("Add")
                removeBox.checked = false;
                editBox.checked = false;
                deleteBox.checked = false;
            }else if(index===2){
                mainButton.text = qsTr("Remove")
                addCheckBox.checked = false;
                editBox.checked = false;
                deleteBox.checked = false;
            }else if(index===3){
                mainButton.text = qsTr("Edit")
                addCheckBox.checked = false;
                removeBox.checked = false;
                deleteBox.checked = false;
            }else if(index===4){
                mainButton.text = qsTr("Delete")
                addCheckBox.checked = false;
                removeBox.checked = false;
                editBox.checked = false;
            }
        }

        Item{
            anchors.fill: parent
            ColumnLayout{
                anchors.fill: parent
                anchors.centerIn: parent
                RowLayout{
                    Label{
                        text: qsTr("Add")
                    }
                    CheckBox{
                         id:addCheckBox
                         Layout.alignment: Qt.AlignRight
                         checkable: true
                         checked: true
                         onCheckedChanged: {
                             if(checked)
                                 index.visible= false;
                         }
                    }
                    Label{
                        text: qsTr("Remove")
                    }
                    CheckBox{
                         id:removeBox
                         Layout.alignment: Qt.AlignRight
                         checkable: true
                         onCheckedChanged: {
                             if(checked)
                                 index.visible= true
                         }
                    }
                    Label{
                        text: qsTr("Edit")
                    }
                    CheckBox{
                        id:editBox
                         Layout.alignment: Qt.AlignRight
                         checkable: true
                         onCheckedChanged: {
                             if(checked)
                                 index.visible= true
                         }
                    }
                    Label{
                        text: qsTr("Delete")
                    }
                    CheckBox{
                         id:deleteBox
                         Layout.alignment: Qt.AlignRight
                         checkable: true
                         onCheckedChanged: {
                             if(checked)
                                 index.visible = true
                         }
                    }
                }
                TextField{
                    id:index
                    width: parent.width
                    visible: false
                    placeholderText: qsTr("Enter index of control")
                }

                TextField{
                    id:about
                    width: parent.width
                    placeholderText: qsTr("Enter About Control")
                }
                TextField{
                    id:description
                    width: parent.width
                    placeholderText: qsTr("Enter Description Control")
                }
                RowLayout{
                    Layout.alignment: Qt.AlignHCenter
                    Button{
                        id:mainButton
                        text: qsTr("Add")
                        highlighted: true
                        onClicked: {
                            if(description.text && about.text){
                                mylist.model.append({'about':about.text,'description':description.text})
                                about.text = "";
                                description.text = "";
                                addDialog.close()
                            }
                        }
                    }
                    //fruitModel.setProperty(index, "cost", cost * 2)
                }
            }
        }
    }

    ListView{
        id:mylist
        width:parent.width
        height:parent.height-addbutton.height
        model: MyListModel{}
        delegate: MyListDelegate{}
        anchors{
            left: parent.left
            right: parent.right
            bottomMargin: 10
        }
        clip: true
    }
    RoundButton{
        id:addbutton
        anchors{
            top: mylist.bottom
            horizontalCenter: parent.horizontalCenter
            bottomMargin: 10
        }
        radius: height/2
        text: qsTr("+")
        onClicked: {
            addDialog.open()
        }
    }
}

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 :

πŸ§‘β€πŸ’»
πŸ“˜
ListElement
count
append
insert
move
remove
set
ListElement
setProperty