The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensional list models .
Detailed Description
QAbstractListModel provides a standard interface for models that represent their data as a simple non-hierarchical sequence of items. It is not used directly, but must be subclassed.
Subclassing
So Letβs Create a class which is abstract from QAbstractListModel.
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12
import QtQuick.Dialogs
import QtQuick.Controls.Imagine 2.3
/* Here you need to follow the instruction
For Qt5.15.x --> import QtQuick.Dialogs 1.3
For Qt6.x.x --> import QtQuick.Dialogs
Note : I am using Qt6.3.x so i imported --> import QtQuick.Dialogs
*/
ApplicationWindow {
visible: true
width: 400
height: 600
minimumWidth: 400
minimumHeight: 600
title: qsTr("blogs.thearticleof.com")
Label{
text: qsTr("blogs.thearticleof.com")
font.pixelSize: 34
color: "grey"
opacity: 0.5
rotation: 360-75
anchors.centerIn: parent
anchors.topMargin: 200
}
ColumnLayout{
anchors.fill: parent
anchors.topMargin: 10
ListView{
id : mListView
Layout.fillWidth: true
Layout.fillHeight: true
model : myModel
delegate:RowLayout{
width: parent.width
spacing: 10
Layout.margins: 10
TextField{
text : names
Layout.fillWidth: true
Layout.leftMargin: 10
onEditingFinished: {
console.log("Editing finished, new text is :"+ text + " at index :" + index)
model.names = text //The roles here are defined in model class
}
}
SpinBox{
id : mSpinbox
editable: true
Layout.fillWidth: true
onValueChanged: {
model.age = value;
}
Component.onCompleted: {
mSpinbox.value = model.age
}
}
Rectangle{
width : 30
height: 30
radius: width/2
color: model.favoriteColor
Layout.rightMargin: 10
MouseArea{
anchors.fill: parent
ColorDialog{
id: colorDialog
title: "Please choose a color"
onAccepted: {
console.log("You choose: " + colorDialog.color)
model.favoriteColor = colorDialog.color
}
onRejected: {
console.log("Canceled")
}
}
onClicked: {
colorDialog.open()
}
}
}
}
}
RowLayout{
width : parent.width
Button{
Layout.fillWidth: true
height: 50
text : "Add Person";
highlighted: true
onClicked: {
input.openDialog()
}
InputDialog{
id : input
onInputDialogAccepted: {
console.log("Here in main, dialog accepted");
console.log( " names : " + personNames + " age :" + personAge)
myModel.addPerson(personNames,personAge)
}
}
}
Button{
Layout.fillWidth: true
height: 50
highlighted: true
text : "Remove Last";
onClicked: {
myModel.removeLastPerson()
}
}
}
}
}
Letβs try build and run the project
Letβs try to add one person :
After Adding Person Look Model Updated :
Since the model provides a more specialized interface than , it is not suitable for use with tree views; you will need to subclass if you want to provide a model for that purpose. If you need to use a number of list models to manage data, it may be more appropriate to subclass instead.
Simple models can be created by subclassing this class and implementing the minimum number of required functions. For example, we could implement a simple read-only -based model that provides a list of strings to a widget. In such a case, we only need to implement the () function to return the number of items in the list, and the () function to retrieve items from the list.
Since the model represents a one-dimensional structure, the () function returns the total number of items in the model. The () function is implemented for interoperability with all kinds of views, but by default informs views that the model contains only one column.
When subclassing QAbstractListModel, you must provide implementations of the () and () functions. Well behaved models also provide a () implementation.
If your model is used within QML and requires roles other than the default ones provided by the () function, you must override it.
For editable list models, you must also provide an implementation of (), and implement the () function so that it returns a value containing .
Note that QAbstractListModel provides a default implementation of () that informs views that there is only a single column of items in this model.
Models that provide interfaces to resizable list-like data structures can provide implementations of () and (). When implementing these functions, it is important to call the appropriate functions so that all connected views are aware of any changes:
An () implementation must call () before inserting new rows into the data structure, and it must call () immediately afterwards.
A () implementation must call () before the rows are removed from the data structure, and it must call () immediately afterwards.