A tree model for QObject* items, usable from both C++ and QML. More...
#include <QmlObjectTreeModel.h>
Public Member Functions | |
| QmlObjectTreeModel (QObject *parent=nullptr) | |
| ~QmlObjectTreeModel () override | |
| int | count () const override |
| void | setDirty (bool dirty) override |
| void | clear () override |
| QModelIndex | index (int row, int column, const QModelIndex &parent=QModelIndex()) const override |
| QModelIndex | parent (const QModelIndex &child) const override |
| int | rowCount (const QModelIndex &parent=QModelIndex()) const override |
| int | columnCount (const QModelIndex &parent=QModelIndex()) const override |
| bool | hasChildren (const QModelIndex &parent=QModelIndex()) const override |
| QVariant | data (const QModelIndex &index, int role=Qt::DisplayRole) const override |
| bool | setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override |
| bool | insertRows (int row, int count, const QModelIndex &parent=QModelIndex()) override |
| bool | removeRows (int row, int count, const QModelIndex &parent=QModelIndex()) override |
| QHash< int, QByteArray > | roleNames () const override |
| Q_INVOKABLE QObject * | getObject (const QModelIndex &index) const |
Returns the QObject* stored at index, or nullptr if invalid. | |
| Q_INVOKABLE QModelIndex | appendItem (QObject *object, const QModelIndex &parentIndex=QModelIndex()) |
Appends object as the last child of parentIndex (root if invalid). Returns the new item's index. | |
| Q_INVOKABLE QModelIndex | appendItem (QObject *object, const QModelIndex &parentIndex, const QString &nodeType) |
| Same as above but also sets a nodeType tag on the created tree node. | |
| Q_INVOKABLE QModelIndex | insertItem (int row, QObject *object, const QModelIndex &parentIndex=QModelIndex()) |
Inserts object at row under parentIndex. Returns the new item's index. | |
| Q_INVOKABLE QModelIndex | insertItem (int row, QObject *object, const QModelIndex &parentIndex, const QString &nodeType) |
| Same as above but also sets a nodeType tag on the created tree node. | |
| Q_INVOKABLE QObject * | removeItem (const QModelIndex &index) |
| Q_INVOKABLE int | childCount (const QModelIndex &parentIndex=QModelIndex()) const |
Number of direct children under parentIndex. | |
| Q_INVOKABLE QModelIndex | childIndex (int row, const QModelIndex &parentIndex=QModelIndex()) const |
Returns the QModelIndex for the child at row under parentIndex. | |
| Q_INVOKABLE QModelIndex | indexForObject (QObject *object) const |
Searches the entire tree for object and returns its QModelIndex (invalid if not found). | |
| Q_INVOKABLE QModelIndex | parentIndex (const QModelIndex &index) const |
| Convenience wrapper around parent(). | |
| Q_INVOKABLE int | depth (const QModelIndex &index) const |
Returns the depth of index (0 = root-level item, -1 = invalid index). | |
| void | appendRootItem (QObject *object) |
| void | appendChild (const QModelIndex &parentIndex, QObject *object) |
| QObject * | removeAt (const QModelIndex &parentIndex, int row) |
| void | removeChildren (const QModelIndex &parentIndex) |
| Removes all children of parentIndex without removing the parent itself. | |
| void | clearAndDeleteContents () |
| Clears the tree and calls deleteLater on every QObject. | |
| bool | contains (QObject *object) const |
| Public Member Functions inherited from ObjectItemModelBase | |
| ObjectItemModelBase (QObject *parent=nullptr) | |
| ~ObjectItemModelBase () override | |
| bool | dirty () const |
| void | beginResetModel () |
| Depth-counted beginResetModel — only the outermost call has effect. | |
| void | endResetModel () |
| Depth-counted endResetModel — only the outermost call has effect. | |
| virtual bool | isEmpty () const |
Static Public Attributes | |
| static constexpr int | NodeTypeRole = Qt::UserRole + 2 |
| static constexpr int | SeparatorRole = Qt::UserRole + 3 |
Additional Inherited Members | |
| Signals inherited from ObjectItemModelBase | |
| void | countChanged (int count) |
| void | dirtyChanged (bool dirty) |
| Protected Slots inherited from ObjectItemModelBase | |
| void | _childDirtyChanged (bool dirty) |
| Protected Member Functions inherited from ObjectItemModelBase | |
| QHash< int, QByteArray > | roleNames () const override |
| void | _signalCountChangedIfNotNested () |
| Protected Attributes inherited from ObjectItemModelBase | |
| bool | _dirty = false |
| uint | _resetModelNestingCount = 0 |
| Static Protected Attributes inherited from ObjectItemModelBase | |
| static constexpr int | ObjectRole = Qt::UserRole |
| static constexpr int | TextRole = Qt::UserRole + 1 |
A tree model for QObject* items, usable from both C++ and QML.
Works like QmlObjectListModel but supports hierarchical parent/child relationships. Compatible with Qt 6 TreeView.
Top-level items are children of an invisible root node. The root is represented by an invalid QModelIndex (the default).
Definition at line 24 of file QmlObjectTreeModel.h.
|
explicit |
Definition at line 64 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::ObjectItemModelBase(), and parent().
|
override |
Definition at line 69 of file QmlObjectTreeModel.cc.
| void QmlObjectTreeModel::appendChild | ( | const QModelIndex & | parentIndex, |
| QObject * | object ) |
Definition at line 381 of file QmlObjectTreeModel.cc.
References appendItem(), and parentIndex().
| QModelIndex QmlObjectTreeModel::appendItem | ( | QObject * | object, |
| const QModelIndex & | parentIndex, | ||
| const QString & | nodeType ) |
Same as above but also sets a nodeType tag on the created tree node.
Definition at line 292 of file QmlObjectTreeModel.cc.
References insertItem(), and parentIndex().
| QModelIndex QmlObjectTreeModel::appendItem | ( | QObject * | object, |
| const QModelIndex & | parentIndex = QModelIndex() ) |
Appends object as the last child of parentIndex (root if invalid). Returns the new item's index.
Definition at line 236 of file QmlObjectTreeModel.cc.
References insertItem(), and parentIndex().
Referenced by appendChild(), and appendRootItem().
| void QmlObjectTreeModel::appendRootItem | ( | QObject * | object | ) |
Definition at line 376 of file QmlObjectTreeModel.cc.
References appendItem().
|
inline |
Number of direct children under parentIndex.
Definition at line 73 of file QmlObjectTreeModel.h.
References parentIndex(), and rowCount().
Referenced by removeChildren().
|
inline |
Returns the QModelIndex for the child at row under parentIndex.
Definition at line 76 of file QmlObjectTreeModel.h.
References index(), and parentIndex().
|
overridevirtual |
Implements ObjectItemModelBase.
Definition at line 432 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::beginResetModel(), and ObjectItemModelBase::endResetModel().
| void QmlObjectTreeModel::clearAndDeleteContents | ( | ) |
Clears the tree and calls deleteLater on every QObject.
Definition at line 445 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::beginResetModel(), and ObjectItemModelBase::endResetModel().
|
override |
Definition at line 114 of file QmlObjectTreeModel.cc.
References parent().
| bool QmlObjectTreeModel::contains | ( | QObject * | object | ) | const |
Definition at line 458 of file QmlObjectTreeModel.cc.
|
overridevirtual |
Implements ObjectItemModelBase.
Definition at line 209 of file QmlObjectTreeModel.cc.
|
override |
Definition at line 126 of file QmlObjectTreeModel.cc.
References index(), NodeTypeRole, ObjectItemModelBase::ObjectRole, SeparatorRole, and ObjectItemModelBase::TextRole.
| int QmlObjectTreeModel::depth | ( | const QModelIndex & | index | ) | const |
Returns the depth of index (0 = root-level item, -1 = invalid index).
Definition at line 357 of file QmlObjectTreeModel.cc.
References index().
| QObject * QmlObjectTreeModel::getObject | ( | const QModelIndex & | index | ) | const |
Returns the QObject* stored at index, or nullptr if invalid.
Definition at line 226 of file QmlObjectTreeModel.cc.
References index().
|
override |
Definition at line 120 of file QmlObjectTreeModel.cc.
References parent().
|
override |
Definition at line 80 of file QmlObjectTreeModel.cc.
References parent().
Referenced by childIndex(), data(), depth(), getObject(), parentIndex(), removeAt(), removeItem(), and setData().
| QModelIndex QmlObjectTreeModel::indexForObject | ( | QObject * | object | ) | const |
Searches the entire tree for object and returns its QModelIndex (invalid if not found).
Definition at line 347 of file QmlObjectTreeModel.cc.
| QModelIndex QmlObjectTreeModel::insertItem | ( | int | row, |
| QObject * | object, | ||
| const QModelIndex & | parentIndex, | ||
| const QString & | nodeType ) |
Same as above but also sets a nodeType tag on the created tree node.
Definition at line 252 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::_resetModelNestingCount, ObjectItemModelBase::_signalCountChangedIfNotNested(), parentIndex(), and setDirty().
| QModelIndex QmlObjectTreeModel::insertItem | ( | int | row, |
| QObject * | object, | ||
| const QModelIndex & | parentIndex = QModelIndex() ) |
Inserts object at row under parentIndex. Returns the new item's index.
Definition at line 247 of file QmlObjectTreeModel.cc.
References insertItem(), and parentIndex().
Referenced by appendItem(), appendItem(), and insertItem().
|
override |
Definition at line 185 of file QmlObjectTreeModel.cc.
|
override |
Definition at line 94 of file QmlObjectTreeModel.cc.
Referenced by columnCount(), hasChildren(), index(), parentIndex(), QmlObjectTreeModel(), and rowCount().
|
inline |
Convenience wrapper around parent().
Definition at line 82 of file QmlObjectTreeModel.h.
References index(), and parent().
Referenced by appendChild(), appendItem(), appendItem(), childCount(), childIndex(), insertItem(), insertItem(), removeAt(), and removeChildren().
| QObject * QmlObjectTreeModel::removeAt | ( | const QModelIndex & | parentIndex, |
| int | row ) |
Definition at line 386 of file QmlObjectTreeModel.cc.
References index(), parentIndex(), and removeItem().
| void QmlObjectTreeModel::removeChildren | ( | const QModelIndex & | parentIndex | ) |
Removes all children of parentIndex without removing the parent itself.
Definition at line 391 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::_resetModelNestingCount, ObjectItemModelBase::_signalCountChangedIfNotNested(), childCount(), and parentIndex().
| QObject * QmlObjectTreeModel::removeItem | ( | const QModelIndex & | index | ) |
Removes the item (and its entire subtree) at index. Returns the removed QObject* (caller takes ownership). Child QObjects are NOT deleted; only the internal tree nodes are freed.
Definition at line 302 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::_resetModelNestingCount, ObjectItemModelBase::_signalCountChangedIfNotNested(), index(), and setDirty().
Referenced by removeAt().
|
override |
Definition at line 191 of file QmlObjectTreeModel.cc.
|
override |
Definition at line 197 of file QmlObjectTreeModel.cc.
References NodeTypeRole, ObjectItemModelBase::roleNames(), and SeparatorRole.
|
override |
Definition at line 108 of file QmlObjectTreeModel.cc.
References parent().
Referenced by childCount().
|
override |
Definition at line 159 of file QmlObjectTreeModel.cc.
References index(), and ObjectItemModelBase::ObjectRole.
|
overridevirtual |
Implements ObjectItemModelBase.
Definition at line 214 of file QmlObjectTreeModel.cc.
References ObjectItemModelBase::_dirty, ObjectItemModelBase::dirty(), and ObjectItemModelBase::dirtyChanged().
Referenced by insertItem(), and removeItem().
|
staticconstexpr |
Definition at line 95 of file QmlObjectTreeModel.h.
Referenced by data(), and roleNames().
|
staticconstexpr |
Definition at line 96 of file QmlObjectTreeModel.h.
Referenced by data(), and roleNames().