easymodel.treemodel.TreeModel

class easymodel.treemodel.TreeModel(root, parent=None)[source]

Bases: PySide.QtCore.QAbstractItemModel

A tree model that uses the TreeItem to represent a general tree.

The model uses TreeItem instances in an hierarchy to build a tree. Each tree item represents a row. The tree items can hold arbitrary ItemData instances.

The model will get automatically updated, when the hierarchy of the tree items changes. You rarely have to use model methods for that. Just use methods of the tree items.

All models need at least a root. The root is responsible for the headers. So the ItemData of the root should have a columns for each header and return a string for them when queried with QtCore.Qt.DisplayRole. Only horizontal headers are supported at the moment. Vertical headers get numbers.

Initialize a new tree model with the given root treeitem

Parameters:
  • root (TreeItem) – the root tree item. The root tree item is responsible for the headers. A ListItemData with the headers is suitable as data for the item.
  • parent (QtCore.QObject) – the parent for the model
Raises:

None

__init__(root, parent=None)[source]

Initialize a new tree model with the given root treeitem

Parameters:
  • root (TreeItem) – the root tree item. The root tree item is responsible for the headers. A ListItemData with the headers is suitable as data for the item.
  • parent (QtCore.QObject) – the parent for the model
Raises:

None

Methods

__init__(root[, parent]) Initialize a new tree model with the given root treeitem
beginInsertColumns
beginInsertRows
beginMoveColumns
beginMoveRows
beginRemoveColumns
beginRemoveRows
beginResetModel
blockSignals
buddy
canFetchMore
changePersistentIndex
changePersistentIndexList
childEvent
children
columnCount(parent) Return the number of columns for the children of the given parent.
connect
connectNotify
createIndex
customEvent
data(index[, role]) Return the data stored under the given role for the item referred to by the index.
decodeData
deleteLater
disconnect
disconnectNotify
dropMimeData
dumpObjectInfo
dumpObjectTree
dynamicPropertyNames
emit
encodeData
endInsertColumns
endInsertRows
endMoveColumns
endMoveRows
endRemoveColumns
endRemoveRows
endResetModel
event
eventFilter
fetchMore
findChild
findChildren
flags(index) Return the flags for the given index
hasChildren
hasIndex
headerData(section, orientation, role) Return the header data
index(row, column[, parent]) Return the index of the item in the model specified by the given row, column and parent index.
index_of_item(item[, column]) Get the index for the given TreeItem
inherits
insertColumn
insertColumns
insertRow(row, item, parent) Insert a single item before the given row in the child items of the parent specified.
insertRows
installEventFilter
isWidgetType
itemData
killTimer
match
metaObject
mimeData
mimeTypes
moveToThread
objectName
parent(index) Return the parent of the model item with the given index.
persistentIndexList
property
receivers
registerUserData
removeColumn
removeColumns
removeEventFilter
removeRow(row, parent) Remove row from parent
removeRows
reset
resetInternalData
revert
roleNames
rowCount(parent) Return the number of rows under the given parent.
sender
senderSignalIndex
setData(index, value[, role]) Set the data of the given index to value
setHeaderData
setItemData
setObjectName
setParent
setProperty
setRoleNames
setSupportedDragActions
sibling
signalsBlocked
sort
span
startTimer
submit
supportedDragActions
supportedDropActions
thread
timerEvent
tr
trUtf8

Attributes

columnsAboutToBeInserted
columnsAboutToBeMoved
columnsAboutToBeRemoved
columnsInserted
columnsMoved
columnsRemoved
dataChanged
destroyed
headerDataChanged
layoutAboutToBeChanged
layoutChanged
modelAboutToBeReset
modelReset
root Return the root tree item
rowsAboutToBeInserted
rowsAboutToBeMoved
rowsAboutToBeRemoved
rowsInserted
rowsMoved
rowsRemoved
staticMetaObject
index(row, column, parent=None)[source]

Return the index of the item in the model specified by the given row, column and parent index.

Parameters:
  • row (int) – the row of the item
  • column (int) – the column for the item
  • parent (QtCore.QModelIndex:) – the parent index
Returns:

the index of the item

Return type:

QtCore.QModelIndex

Raises:

None

parent(index)[source]

Return the parent of the model item with the given index. If the item has no parent, return an invalid QModelIndex.

Parameters:index (QtCore.QModelIndex) – the index that you want to know the parent of
Returns:parent index
Return type:QtCore.QModelIndex
Raises:None
rowCount(parent)[source]

Return the number of rows under the given parent. When the parent is valid return rowCount the number of children of parent.

Parameters:parent (QtCore.QModelIndex:) – the parent index
Returns:the row count
Return type:int
Raises:None
columnCount(parent)[source]

Return the number of columns for the children of the given parent.

Parameters:parent (QtCore.QModelIndex:) – the parent index
Returns:the column count
Return type:int
Raises:None
data(index, role=PySide.QtCore.Qt.ItemDataRole.DisplayRole)[source]

Return the data stored under the given role for the item referred to by the index.

Parameters:
  • index (QtCore.QModelIndex) – the index
  • role (QtCore.Qt.ItemDataRole) – the data role
Returns:

some data depending on the role

Raises:

None

setData(index, value, role=PySide.QtCore.Qt.ItemDataRole.EditRole)[source]

Set the data of the given index to value

Parameters:
  • index (QtCore.QModelIndex) – the index to set
  • value – the value to set
  • role (QtCore.Qt.ItemDataRole) – the role, usually edit role
Returns:

True, if successfull, False if unsuccessfull

Return type:

bool

Raises:

None

headerData(section, orientation, role)[source]

Return the header data

Will call TreeItem.data() of the root TreeItem with the given section (column) and role for horizontal orientations.

Vertical orientations are numbered.

Parameters:
  • section (int) – the section in the header view
  • orientation (QtCore.Qt.Vertical | QtCore.Qt.Horizontal) – vertical or horizontal orientation
  • role (QtCore.Qt.ItemDataRole) – the data role.
Returns:

data for the header

Raises:

None

insertRow(row, item, parent)[source]

Insert a single item before the given row in the child items of the parent specified.

Parameters:
  • row (int) – the index where the rows get inserted
  • item (TreeItem) – the item to insert. When creating the item, make sure it’s parent is None. If not it will defeat the purpose of this function.
  • parent (QtCore.QModelIndex) – the parent
Returns:

Returns true if the row is inserted; otherwise returns false.

Return type:

bool

Raises:

None

removeRow(row, parent)[source]

Remove row from parent

Parameters:
  • row (int) – the row index
  • parent (QtCore.QModelIndex) – the parent index
Returns:

True if row is inserted; otherwise returns false.

Return type:

bool

Raises:

None

root

Return the root tree item

Returns:the root item
Return type:TreeItem
Raises:None
flags(index)[source]

Return the flags for the given index

This will call TreeItem.flags() for valid ones.

Parameters:index (QtCore.QModelIndex) – the index to query
Returns:None
Return type:None
Raises:None
staticMetaObject = <PySide.QtCore.QMetaObject object>
index_of_item(item, column=0)[source]

Get the index for the given TreeItem

Parameters:
  • item (TreeItem) – the treeitem to query
  • column (int) – the column of the index
Returns:

the index of the item

Return type:

QtCore.QModelIndex

Raises:

ValueError