Qt Qml Abstract Item Model using C++

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.

Since the model provides a more specialized interface than QAbstractItemModel, it is not suitable for use with tree views; you will need to subclass QAbstractItemModel 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 QAbstractTableModel 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 QStringList-based model that provides a list of strings to a QListView widget. In such a case, we only need to implement the rowCount() function to return the number of items in the list, and the data() function to retrieve items from the list.

Since the model represents a one-dimensional structure, the rowCount() function returns the total number of items in the model. The columnCount() function is implemented for interoperability with all kinds of views, but by default informs views that the model contains only one column.

Subclassing

When subclassing QAbstractListModel, you must provide implementations of the rowCount() and data() functions. Well behaved models also provide a headerData() implementation.

If your model is used within QML and requires roles other than the default ones provided by the roleNames() function, you must override it.

For editable list models, you must also provide an implementation of setData(), and implement the flags() function so that it returns a value containing Qt::ItemIsEditable.

Note that QAbstractListModel provides a default implementation of columnCount() 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 insertRows() and removeRows(). When implementing these functions, it is important to call the appropriate functions so that all connected views are aware of any changes:

So Let’s Create a class which is abstract from QAbstractListModel.

Let’s Look at the code :

personmodel.cpp

personmodel.h

person.h

person.cpp

Let’s register model into main.cpp

Create Input Dialog called InputDialog.qml

Final Code main.qml

Let’s try build and run the project

main app after run

Let’s try to add one person :

After Adding Person Look Model Updated :

Note: You can find the whole source code on my github.

Last updated

Was this helpful?