Skip to content

PythonQt.QtGui.QStandardItemModel

QStandardItemModel

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

QStandardItemModel can be used as a repository for standard Qt data types. It is one of the Model/View Classes and is part of Qt's model/view framework.

QStandardItemModel provides a classic item-based approach to working with the model. The items in a QStandardItemModel are provided by QStandardItem.

QStandardItemModel implements the QAbstractItemModel interface, which means that the model can be used to provide data in any view that supports that interface (such as QListView, QTableView and QTreeView, and your own custom views). For performance and flexibility, you may want to subclass QAbstractItemModel to provide support for different kinds of data repositories. For example, the QDirModel provides a model interface to the underlying file system.

When you want a list or tree, you typically create an empty QStandardItemModel and use appendRow() to add items to the model, and item() to access an item. If your model represents a table, you typically pass the dimensions of the table to the QStandardItemModel constructor and use setItem() to position items into the table. You can also use setRowCount() and setColumnCount() to alter the dimensions of the model. To insert items, use insertRow() or insertColumn(), and to remove items, use removeRow() or removeColumn().

You can set the header labels of your model with setHorizontalHeaderLabels() and setVerticalHeaderLabels().

You can search for items in the model with findItems(), and sort the model by calling sort().

Call clear() to remove all items from the model.

An example usage of QStandardItemModel to create a table:

QStandardItemModel model(4, 4);
for (int row = 0; row < model.rowCount(); ++row) {
    for (int column = 0; column < model.columnCount(); ++column) {
        QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
        model.setItem(row, column, item);
    }
}

An example usage of QStandardItemModel to create a tree:

QStandardItemModel model;
QStandardItem *parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i) {
    QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    parentItem->appendRow(item);
    parentItem = item;
}

After setting the model on a view, you typically want to react to user actions, such as an item being clicked. Since a QAbstractItemView provides QModelIndex-based signals and functions, you need a way to obtain the QStandardItem that corresponds to a given QModelIndex, and vice versa. itemFromIndex() and indexFromItem() provide this mapping. Typical usage of itemFromIndex() includes obtaining the item at the current index in a view, and obtaining the item that corresponds to an index carried by a QAbstractItemView signal, such as QAbstractItemView::clicked(). First you connect the view's signal to a slot in your class:

QTreeView *treeView = new QTreeView(this);
treeView->setModel(myStandardItemModel);
connect(treeView, &QTreeView::clicked,
        this, &MyWidget::clicked);

When you receive the signal, you call itemFromIndex() on the given model index to get a pointer to the item:

void MyWidget::clicked(const QModelIndex &index)
{
    QStandardItem *item = myStandardItemModel->itemFromIndex(index);
    // Do stuff with the item ...
}

Conversely, you must obtain the QModelIndex of an item when you want to invoke a model/view function that takes an index as argument. You can obtain the index either by using the model's indexFromItem() function, or, equivalently, by calling QStandardItem::index():

treeView->scrollTo(item->index());

You are, of course, not required to use the item-based approach; you could instead rely entirely on the QAbstractItemModel interface when working with the model, or use a combination of the two as appropriate.

Source: https://doc.qt.io/archives/qt-5.15/qstandarditemmodel.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. (shared captured text)
DoNotUseParent attribute Any (shared captured text)
HorizontalSortHint attribute Any (shared captured text)
IndexIsValid attribute Any (shared captured text)
LayoutChangeHint attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
NoLayoutChangeHint attribute Any (shared captured text)
NoOption attribute Any (shared captured text)
ParentIsInvalid attribute Any (shared captured text)
VerticalSortHint attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
appendColumn function (self, items: Any) -> None Appends a column containing items.
appendRow function (self, item: Any) -> None Appends a row containing items.
beginInsertColumns function (self, parent: Any, first: Any, last: Any) -> Any X.beginInsertColumns(parent, first, last) (shared captured text)
beginInsertRows function (self, parent: Any, first: Any, last: Any) -> Any X.beginInsertRows(parent, first, last) (shared captured text)
beginMoveColumns function (self, sourceParent: Any, sourceFirst: Any, sourceLast: Any, destinationParent: Any, destinationColumn: Any) … X.beginMoveColumns(sourceParent, sourceFirst, sourceLast, destinationParent, destinationColumn) -> bool (shared captured text)
beginMoveRows function (self, sourceParent: Any, sourceFirst: Any, sourceLast: Any, destinationParent: Any, destinationRow: Any) -> … X.beginMoveRows(sourceParent, sourceFirst, sourceLast, destinationParent, destinationRow) -> bool (shared captured text)
beginRemoveColumns function (self, parent: Any, first: Any, last: Any) -> Any X.beginRemoveColumns(parent, first, last) (shared captured text)
beginRemoveRows function (self, parent: Any, first: Any, last: Any) -> Any X.beginRemoveRows(parent, first, last) (shared captured text)
beginResetModel function (self) -> Any (shared captured text)
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 (shared captured text)
canDropMimeData function (self, data: Any, action: Any, row: Any, column: Any, parent: Any) -> bool X.canDropMimeData(data, action, row, column, parent) -> bool (shared captured text)
canFetchMore function (self, parent: Any) -> bool X.canFetchMore(parent) -> bool (shared captured text)
changePersistentIndex function (self, *args: Any, **kwargs: Any) -> Any X.changePersistentIndex(from, to) (shared captured text)
changePersistentIndexList function (self, *args: Any, **kwargs: Any) -> Any X.changePersistentIndexList(from, to) (shared captured text)
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)
clear function (self) -> None Removes all items (including header items) from the model and sets the number of rows and columns to zero.
columnCount function (self, parent: Any) -> int X.columnCount(parent) -> int
columnsAboutToBeInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.columnsAboutToBeInserted; argument details are not documented.
columnsAboutToBeMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.columnsAboutToBeMoved; argument details are not documented.
columnsAboutToBeRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.columnsAboutToBeRemoved; argument details are not documented.
columnsInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.columnsInserted; argument details are not documented.
columnsMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.columnsMoved; argument details are not documented.
columnsRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.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: Any, column: Any, id: Any) -> Any X.createIndex(row, column, id) -> PythonQt.QtCore.QModelIndex (shared captured text)
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.QtGui.QStandardItemModel.dataChanged; argument details are not documented.
decodeData function (self, row: Any, column: Any, parent: Any, stream: Any) -> bool X.decodeData(row, column, parent, stream) -> bool (shared captured text)
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.QtGui.QStandardItemModel.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) -> Any X.encodeData(indexes, stream) (shared captured text)
endInsertColumns function (self) -> Any (shared captured text)
endInsertRows function (self) -> Any (shared captured text)
endMoveColumns function (self) -> Any (shared captured text)
endMoveRows function (self) -> Any (shared captured text)
endRemoveColumns function (self) -> Any (shared captured text)
endRemoveRows function (self) -> Any (shared captured text)
endResetModel function (self) -> Any (shared captured text)
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
fetchMore function (self, parent: Any) -> Any (shared captured text)
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)
findItems function (self, text: str, arg1: flags, column: int) -> tuple X.findItems(text, flags, column) -> tuple
flags function (self, index: Any) -> Any Reimplements: [QAbstractItemModel::flags](https://doc.qt.io/archives/qt-5.15/qabstractitemmodel.html#flags)(const QModelIndex \&index) const.
hasChildren function (self, parent: Any) -> bool X.hasChildren(parent) -> bool
hasIndex function (self, row: Any, column: Any, parent: Any) -> bool X.hasIndex(row, column, parent) -> bool (shared captured text)
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.QtGui.QStandardItemModel.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)
horizontalHeaderItem function (self, column: int) -> Any X.horizontalHeaderItem(column) -> PythonQt.QtGui.QStandardItem
index function (self, row: int, column: int, parent: Any) -> Any X.index(row, column, parent) -> PythonQt.QtCore.QModelIndex
indexFromItem function (self, item: Any) -> Any X.indexFromItem(item) -> 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, items: Any) -> Any X.insertColumn(column, items)
insertColumns function (self, column: int, count: int, parent: Any) -> bool X.insertColumns(column, count, parent) -> bool
insertRow function (self, row: int, item: Any) -> Any X.insertRow(row, item)
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)
invisibleRootItem function (self) -> Any X.invisibleRootItem() -> PythonQt.QtGui.QStandardItem
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)
item function (self, row: int, column: int) -> Any X.item(row, column) -> PythonQt.QtGui.QStandardItem
itemChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.itemChanged; argument details are not documented.
itemData function (self, index: Any) -> dict X.itemData(index) -> dict
itemFromIndex function (self, index: Any) -> Any X.itemFromIndex(index) -> PythonQt.QtGui.QStandardItem
itemPrototype function (self) -> Any X.itemPrototype() -> PythonQt.QtGui.QStandardItem
killTimer function (self, id: Any) -> Any (shared captured text)
layoutAboutToBeChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.layoutAboutToBeChanged; argument details are not documented.
layoutChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.layoutChanged; argument details are not documented.
match function (self, start: Any, role: Any, value: Any, hits: Any, arg4: flags) -> tuple X.match(start, role, value, hits, flags) -> tuple (shared captured text)
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.QtGui.QStandardItemModel.modelAboutToBeReset; argument details are not documented.
modelReset function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.modelReset; argument details are not documented.
moveColumn function (self, sourceParent: Any, sourceColumn: Any, destinationParent: Any, destinationChild: Any) -> bool X.moveColumn(sourceParent, sourceColumn, destinationParent, destinationChild) -> bool (shared captured text)
moveColumns function (self, sourceParent: Any, sourceColumn: Any, count: Any, destinationParent: Any, destinationChild: Any) -> bo… X.moveColumns(sourceParent, sourceColumn, count, destinationParent, destinationChild) -> bool (shared captured text)
moveRow function (self, sourceParent: Any, sourceRow: Any, destinationParent: Any, destinationChild: Any) -> bool X.moveRow(sourceParent, sourceRow, destinationParent, destinationChild) -> bool (shared captured text)
moveRows function (self, sourceParent: Any, sourceRow: Any, count: Any, destinationParent: Any, destinationChild: Any) -> bool X.moveRows(sourceParent, sourceRow, count, destinationParent, destinationChild) -> bool (shared captured text)
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.QtGui.QStandardItemModel.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 (shared captured text)
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
removeColumn function (self, column: Any, parent: Any) -> bool X.removeColumn(column, parent) -> bool (shared captured text)
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: Any, parent: Any) -> bool X.removeRow(row, parent) -> bool (shared captured text)
removeRows function (self, row: int, count: int, parent: Any) -> bool X.removeRows(row, count, parent) -> bool
revert function (self) -> Any (shared captured text)
roleNames function (self) -> dict X.roleNames() -> dict (shared captured text)
rowCount function (self, parent: Any) -> int X.rowCount(parent) -> int
rowsAboutToBeInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.rowsAboutToBeInserted; argument details are not documented.
rowsAboutToBeMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.rowsAboutToBeMoved; argument details are not documented.
rowsAboutToBeRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.rowsAboutToBeRemoved; argument details are not documented.
rowsInserted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.rowsInserted; argument details are not documented.
rowsMoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.rowsMoved; argument details are not documented.
rowsRemoved function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QStandardItemModel.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)
setColumnCount function (self, columns: int) -> None Sets the number of columns in this model to columns.
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
setHorizontalHeaderItem function (self, column: int, item: Any) -> None X.setHorizontalHeaderItem(column, item)
setHorizontalHeaderLabels function (self, labels: Any) -> None Sets the horizontal header labels using labels.
setItem function (self, row: int, column: Any, item: Any) -> None X.setItem(row, column, item)
setItemData function (self, index: Any, roles: Any) -> bool X.setItemData(index, roles) -> bool
setItemPrototype function (self, item: Any) -> None Sets the item prototype for the model to the specified item.
setItemRoleNames function (self, roleNames: Any) -> None Sets the item role names to roleNames.
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)
setRowCount function (self, rows: int) -> None Sets the number of rows in this model to rows.
setSortRole function (self, role: int) -> None
setVerticalHeaderItem function (self, row: int, item: Any) -> None X.setVerticalHeaderItem(row, item)
setVerticalHeaderLabels function (self, labels: Any) -> None Sets the vertical header labels using labels.
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)
sortRole attribute int This property holds the item role that is used to query the model's data when sorting items
span function (self, index: Any) -> Any X.span(index) -> PythonQt.QtCore.QSize (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
submit function (self) -> bool X.submit() -> bool (shared captured text)
supportedDragActions function (self) -> Any (shared captured text)
supportedDropActions function (self) -> Any Reimplements: [QAbstractItemModel::supportedDropActions](https://doc.qt.io/archives/qt-5.15/qabstractitemmodel.html#supportedDropActions)() const.
takeColumn function (self, column: int) -> tuple X.takeColumn(column) -> tuple
takeHorizontalHeaderItem function (self, column: int) -> Any X.takeHorizontalHeaderItem(column) -> PythonQt.QtGui.QStandardItem
takeItem function (self, row: int, column: int) -> Any X.takeItem(row, column) -> PythonQt.QtGui.QStandardItem
takeRow function (self, row: int) -> tuple X.takeRow(row) -> tuple
takeVerticalHeaderItem function (self, row: int) -> Any X.takeVerticalHeaderItem(row) -> PythonQt.QtGui.QStandardItem
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)
verticalHeaderItem function (self, row: int) -> Any X.verticalHeaderItem(row) -> PythonQt.QtGui.QStandardItem