Skip to content

PythonQt.QtCore.QAbstractItemModel

QAbstractItemModel

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

The QAbstractItemModel class defines the standard interface that item models must use to be able to interoperate with other components in the model/view architecture. It is not supposed to be instantiated directly. Instead, you should subclass it to create new models.

The QAbstractItemModel class is one of the Model/View Classes and is part of Qt's model/view framework. It can be used as the underlying data model for the item view elements in QML or the item view classes in the Qt Widgets module.

If you need a model to use with an item view such as QML's List View element or the C++ widgets QListView or QTableView, you should consider subclassing QAbstractListModel or QAbstractTableModel instead of this class.

The underlying data model is exposed to views and delegates as a hierarchy of tables. If you do not make use of the hierarchy, then the model is a simple table of rows and columns. Each item has a unique index specified by a QModelIndex.

Every item of data that can be accessed via a model has an associated model index. You can obtain this model index using the index() function. Each index may have a sibling() index; child items have a parent() index.

Each item has a number of data elements associated with it and they can be retrieved by specifying a role (see Qt::ItemDataRole) to the model's data() function. Data for all available roles can be obtained at the same time using the itemData() function.

Data for each role is set using a particular Qt::ItemDataRole. Data for individual roles are set individually with setData(), or they can be set for all roles with setItemData().

Items can be queried with flags() (see Qt::ItemFlag) to see if they can be selected, dragged, or manipulated in other ways.

If an item has child objects, hasChildren() returns true for the corresponding index.

The model has a rowCount() and a columnCount() for each level of the hierarchy. Rows and columns can be inserted and removed with insertRows(), insertColumns(), removeRows(), and removeColumns().

The model emits signals to indicate changes. For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account.

The items available through the model can be searched for particular data using the match() function.

To sort the model, you can use sort().

Subclassing

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

When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). These functions are used in all read-only models, and form the basis of editable models.

You can also reimplement hasChildren() to provide special behavior for models where the implementation of rowCount() is expensive. This makes it possible for models to restrict the amount of data requested by views, and can be used as a way to implement lazy population of model data.

To enable editing in your model, you must also implement setData(), and reimplement flags() to ensure that ItemIsEditable is returned. You can also reimplement headerData() and setHeaderData() to control the way the headers for your model are presented.

The dataChanged() and headerDataChanged() signals must be emitted explicitly when reimplementing the setData() and setHeaderData() functions, respectively.

Custom models need to create model indexes for other components to use. To do this, call createIndex() with suitable row and column numbers for the item, and an identifier for it, either as a pointer or as an integer value. The combination of these values must be unique for each item. Custom models typically use these unique identifiers in other reimplemented functions to retrieve item data and access information about the item's parents and children. See the Simple Tree Model Example for more information about unique identifiers.

It is not necessary to support every role defined in Qt::ItemDataRole. Depending on the type of data contained within a model, it may only be useful to implement the data() function to return valid information for some of the more common roles. Most models provide at least a textual representation of item data for the Qt::DisplayRole, and well-behaved models should also provide valid information for the Qt::ToolTipRole and Qt::WhatsThisRole. Supporting these roles enables models to be used with standard Qt views. However, for some models that handle highly-specialized data, it may be appropriate to provide data only for user-defined roles.

Models that provide interfaces to resizable data structures can provide implementations of insertRows(), removeRows(), insertColumns(),and removeColumns(). When implementing these functions, it is important to notify any connected views about changes to the model's dimensions both before and after they occur:

The private signals that these functions emit give attached components the chance to take action before any data becomes unavailable. The encapsulation of the insert and remove operations with these begin and end functions also enables the model to manage persistent model indexes correctly. If you want selections to be handled properly, you must ensure that you call these functions. If you insert or remove an item with children, you do not need to call these functions for the child items. In other words, the parent item will take care of its child items.

To create models that populate incrementally, you can reimplement fetchMore() and canFetchMore(). If the reimplementation of fetchMore() adds rows to the model, beginInsertRows() and endInsertRows() must be called.

Source: https://doc.qt.io/archives/qt-5.15/qabstractitemmodel.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
CheckIndexOption attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
DoNotUseParent attribute Any
HorizontalSortHint attribute Any
IndexIsValid attribute Any
LayoutChangeHint attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
NoLayoutChangeHint attribute Any
NoOption attribute Any
ParentIsInvalid attribute Any
VerticalSortHint attribute Any
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
beginInsertColumns function (self, parent: Any, first: int, last: int) -> None X.beginInsertColumns(parent, first, last)
beginInsertRows function (self, parent: Any, first: int, last: int) -> None X.beginInsertRows(parent, first, last)
beginMoveColumns function (self, sourceParent: Any, sourceFirst: int, sourceLast: int, destinationParent: Any, destinationColumn: int) … X.beginMoveColumns(sourceParent, sourceFirst, sourceLast, destinationParent, destinationColumn) -> bool
beginMoveRows function (self, sourceParent: Any, sourceFirst: int, sourceLast: int, destinationParent: Any, destinationRow: int) -> … X.beginMoveRows(sourceParent, sourceFirst, sourceLast, destinationParent, destinationRow) -> bool
beginRemoveColumns function (self, parent: Any, first: int, last: int) -> None X.beginRemoveColumns(parent, first, last)
beginRemoveRows function (self, parent: Any, first: int, last: int) -> None X.beginRemoveRows(parent, first, last)
beginResetModel function (self) -> None Begins a model reset operation.
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
buddy function (self, index: Any) -> Any X.buddy(index) -> PythonQt.QtCore.QModelIndex
canDropMimeData function (self, data: Any, action: Any, row: int, column: int, parent: Any) -> bool X.canDropMimeData(data, action, row, column, parent) -> bool
canFetchMore function (self, parent: Any) -> bool X.canFetchMore(parent) -> bool
changePersistentIndex function (self, *args: Any, **kwargs: Any) -> None X.changePersistentIndex(from, to)
changePersistentIndexList function (self, *args: Any, **kwargs: Any) -> None X.changePersistentIndexList(from, to)
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
columnCount function (self, parent: Any) -> int X.columnCount(parent) -> int
columnsAboutToBeInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsAboutToBeInserted; argument details are not documented.
columnsAboutToBeMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsAboutToBeMoved; argument details are not documented.
columnsAboutToBeRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsAboutToBeRemoved; argument details are not documented.
columnsInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsInserted; argument details are not documented.
columnsMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsMoved; argument details are not documented.
columnsRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.columnsRemoved; argument details are not documented.
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
createIndex function (self, row: int, column: int, id: Any) -> Any X.createIndex(row, column, id) -> PythonQt.QtCore.QModelIndex
customEvent function (self, event: Any) -> Any (shared captured text)
data function (self, index: Any, role: int) -> Any X.data(index, role) -> object
dataChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.dataChanged; argument details are not documented.
decodeData function (self, row: int, column: int, parent: Any, stream: Any) -> bool X.decodeData(row, column, parent, stream) -> bool
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.destroyed; argument details are not documented.
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
dropMimeData function (self, data: Any, action: Any, row: int, column: int, parent: Any) -> bool X.dropMimeData(data, action, row, column, parent) -> bool
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
encodeData function (self, indexes: Any, stream: Any) -> None X.encodeData(indexes, stream)
endInsertColumns function (self) -> None Ends a column insertion operation.
endInsertRows function (self) -> None Ends a row insertion operation.
endMoveColumns function (self) -> None Ends a column move operation.
endMoveRows function (self) -> None Ends a row move operation.
endRemoveColumns function (self) -> None Ends a column removal operation.
endRemoveRows function (self) -> None Ends a row removal operation.
endResetModel function (self) -> None Completes a model reset operation.
event function (self, event: Any) -> bool X.event(event) -> bool
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
fetchMore function (self, parent: Any) -> None Fetches any available data for the items with the parent specified by the parent index.
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
flags function (self, index: Any) -> Any Returns the item flags for the given index.
hasChildren function (self, parent: Any) -> bool X.hasChildren(parent) -> bool
hasIndex function (self, row: int, column: int, parent: Any) -> bool X.hasIndex(row, column, parent) -> bool
headerData function (self, section: int, orientation: Any, role: int) -> Any X.headerData(section, orientation, role) -> object
headerDataChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.headerDataChanged; argument details are not documented.
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
index function (self, row: int, column: int, parent: Any) -> Any X.index(row, column, parent) -> PythonQt.QtCore.QModelIndex
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
insertColumn function (self, column: int, parent: Any) -> bool X.insertColumn(column, parent) -> bool
insertColumns function (self, column: int, count: int, parent: Any) -> bool X.insertColumns(column, count, parent) -> bool
insertRow function (self, row: int, parent: Any) -> bool X.insertRow(row, parent) -> bool
insertRows function (self, row: int, count: int, parent: Any) -> bool X.insertRows(row, count, parent) -> bool
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
itemData function (self, index: Any) -> dict X.itemData(index) -> dict
killTimer function (self, id: Any) -> Any (shared captured text)
layoutAboutToBeChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.layoutAboutToBeChanged; argument details are not documented.
layoutChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.layoutChanged; argument details are not documented.
match function (self, start: Any, role: int, value: Any, hits: int, arg4: flags) -> tuple X.match(start, role, value, hits, flags) -> tuple
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
mimeData function (self, indexes: Any) -> Any X.mimeData(indexes) -> PythonQt.QtCore.QMimeData
mimeTypes function (self) -> tuple X.mimeTypes() -> tuple
modelAboutToBeReset function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.modelAboutToBeReset; argument details are not documented.
modelReset function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.modelReset; argument details are not documented.
moveColumn function (self, sourceParent: Any, sourceColumn: int, destinationParent: Any, destinationChild: int) -> bool X.moveColumn(sourceParent, sourceColumn, destinationParent, destinationChild) -> bool
moveColumns function (self, sourceParent: Any, sourceColumn: int, count: int, destinationParent: Any, destinationChild: int) -> bo… X.moveColumns(sourceParent, sourceColumn, count, destinationParent, destinationChild) -> bool
moveRow function (self, sourceParent: Any, sourceRow: int, destinationParent: Any, destinationChild: int) -> bool X.moveRow(sourceParent, sourceRow, destinationParent, destinationChild) -> bool
moveRows function (self, sourceParent: Any, sourceRow: int, count: int, destinationParent: Any, destinationChild: int) -> bool X.moveRows(sourceParent, sourceRow, count, destinationParent, destinationChild) -> bool
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.objectNameChanged; argument details are not documented.
parent function (self, child: Any) -> Any X.parent(child) -> PythonQt.QtCore.QModelIndex
persistentIndexList function (self) -> tuple X.persistentIndexList() -> tuple
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
removeColumn function (self, column: int, parent: Any) -> bool X.removeColumn(column, parent) -> bool
removeColumns function (self, column: int, count: int, parent: Any) -> bool X.removeColumns(column, count, parent) -> bool
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
removeRow function (self, row: int, parent: Any) -> bool X.removeRow(row, parent) -> bool
removeRows function (self, row: int, count: int, parent: Any) -> bool X.removeRows(row, count, parent) -> bool
revert function (self) -> None Lets the model know that it should discard cached information.
roleNames function (self) -> dict X.roleNames() -> dict
rowCount function (self, parent: Any) -> int X.rowCount(parent) -> int
rowsAboutToBeInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsAboutToBeInserted; argument details are not documented.
rowsAboutToBeMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsAboutToBeMoved; argument details are not documented.
rowsAboutToBeRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsAboutToBeRemoved; argument details are not documented.
rowsInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsInserted; argument details are not documented.
rowsMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsMoved; argument details are not documented.
rowsRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtCore.QAbstractItemModel.rowsRemoved; argument details are not documented.
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setData function (self, index: Any, value: Any, role: int) -> bool X.setData(index, value, role) -> bool
setHeaderData function (self, section: int, orientation: Any, value: Any, role: int) -> bool X.setHeaderData(section, orientation, value, role) -> bool
setItemData function (self, index: Any, roles: Any) -> bool X.setItemData(index, roles) -> bool
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
sibling function (self, row: int, column: int, idx: Any) -> Any X.sibling(row, column, idx) -> PythonQt.QtCore.QModelIndex
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
sort function (self, column: int, order: Any) -> None X.sort(column, order)
span function (self, index: Any) -> Any X.span(index) -> PythonQt.QtCore.QSize
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
submit function (self) -> bool X.submit() -> bool
supportedDragActions function (self) -> Any Returns the actions supported by the data in this model.
supportedDropActions function (self) -> Any Returns the drop actions supported by this model.
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)