QAbstractListModel

Inheritance diagram of PySide2.QtCore.QAbstractListModel

Inherited by: QStringListModel, QHelpIndexModel

Detailed Description

The PySide2.QtCore.QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensional list models.

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

Since the model represents a one-dimensional structure, the PySide2.QtCore.QAbstractItemModel.rowCount() function returns the total number of items in the model. The PySide2.QtCore.QAbstractListModel.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 PySide2.QtCore.QAbstractListModel , you must provide implementations of the PySide2.QtCore.QAbstractItemModel.rowCount() and PySide2.QtCore.QAbstractItemModel.data() functions. Well behaved models also provide a PySide2.QtCore.QAbstractItemModel.headerData() implementation.

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

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

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

Note

Some general guidelines for subclassing models are available in the Model Subclassing Reference .

class PySide2.QtCore.QAbstractListModel([parent=nullptr])
Parameters:parentPySide2.QtCore.QObject

Constructs an abstract list model with the given parent .