QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QmlObjectTreeModel Class Reference

A tree model for QObject* items, usable from both C++ and QML. More...

#include <QmlObjectTreeModel.h>

Inheritance diagram for QmlObjectTreeModel:
Collaboration diagram for QmlObjectTreeModel:

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ QmlObjectTreeModel()

QmlObjectTreeModel::QmlObjectTreeModel ( QObject * parent = nullptr)
explicit

Definition at line 64 of file QmlObjectTreeModel.cc.

References ObjectItemModelBase::ObjectItemModelBase(), and parent().

◆ ~QmlObjectTreeModel()

QmlObjectTreeModel::~QmlObjectTreeModel ( )
override

Definition at line 69 of file QmlObjectTreeModel.cc.

Member Function Documentation

◆ appendChild()

void QmlObjectTreeModel::appendChild ( const QModelIndex & parentIndex,
QObject * object )

Definition at line 381 of file QmlObjectTreeModel.cc.

References appendItem(), and parentIndex().

◆ appendItem() [1/2]

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().

◆ appendItem() [2/2]

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().

◆ appendRootItem()

void QmlObjectTreeModel::appendRootItem ( QObject * object)

Definition at line 376 of file QmlObjectTreeModel.cc.

References appendItem().

◆ childCount()

Q_INVOKABLE int QmlObjectTreeModel::childCount ( const QModelIndex & parentIndex = QModelIndex()) const
inline

Number of direct children under parentIndex.

Definition at line 73 of file QmlObjectTreeModel.h.

References parentIndex(), and rowCount().

Referenced by removeChildren().

◆ childIndex()

Q_INVOKABLE QModelIndex QmlObjectTreeModel::childIndex ( int row,
const QModelIndex & parentIndex = QModelIndex() ) const
inline

Returns the QModelIndex for the child at row under parentIndex.

Definition at line 76 of file QmlObjectTreeModel.h.

References index(), and parentIndex().

◆ clear()

void QmlObjectTreeModel::clear ( )
overridevirtual

◆ clearAndDeleteContents()

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().

◆ columnCount()

int QmlObjectTreeModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 114 of file QmlObjectTreeModel.cc.

References parent().

◆ contains()

bool QmlObjectTreeModel::contains ( QObject * object) const

Definition at line 458 of file QmlObjectTreeModel.cc.

◆ count()

int QmlObjectTreeModel::count ( ) const
overridevirtual

Implements ObjectItemModelBase.

Definition at line 209 of file QmlObjectTreeModel.cc.

◆ data()

QVariant QmlObjectTreeModel::data ( const QModelIndex & index,
int role = Qt::DisplayRole ) const
override

◆ depth()

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().

◆ getObject()

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().

◆ hasChildren()

bool QmlObjectTreeModel::hasChildren ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 120 of file QmlObjectTreeModel.cc.

References parent().

◆ index()

QModelIndex QmlObjectTreeModel::index ( int row,
int column,
const QModelIndex & parent = QModelIndex() ) const
override

Definition at line 80 of file QmlObjectTreeModel.cc.

References parent().

Referenced by childIndex(), data(), depth(), getObject(), parentIndex(), removeAt(), removeItem(), and setData().

◆ indexForObject()

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.

◆ insertItem() [1/2]

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().

◆ insertItem() [2/2]

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().

◆ insertRows()

bool QmlObjectTreeModel::insertRows ( int row,
int count,
const QModelIndex & parent = QModelIndex() )
override

Definition at line 185 of file QmlObjectTreeModel.cc.

◆ parent()

QModelIndex QmlObjectTreeModel::parent ( const QModelIndex & child) const
override

◆ parentIndex()

Q_INVOKABLE QModelIndex QmlObjectTreeModel::parentIndex ( const QModelIndex & index) const
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().

◆ removeAt()

QObject * QmlObjectTreeModel::removeAt ( const QModelIndex & parentIndex,
int row )

Definition at line 386 of file QmlObjectTreeModel.cc.

References index(), parentIndex(), and removeItem().

◆ removeChildren()

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().

◆ removeItem()

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().

◆ removeRows()

bool QmlObjectTreeModel::removeRows ( int row,
int count,
const QModelIndex & parent = QModelIndex() )
override

Definition at line 191 of file QmlObjectTreeModel.cc.

◆ roleNames()

QHash< int, QByteArray > QmlObjectTreeModel::roleNames ( ) const
override

◆ rowCount()

int QmlObjectTreeModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 108 of file QmlObjectTreeModel.cc.

References parent().

Referenced by childCount().

◆ setData()

bool QmlObjectTreeModel::setData ( const QModelIndex & index,
const QVariant & value,
int role = Qt::EditRole )
override

Definition at line 159 of file QmlObjectTreeModel.cc.

References index(), and ObjectItemModelBase::ObjectRole.

◆ setDirty()

void QmlObjectTreeModel::setDirty ( bool dirty)
overridevirtual

Member Data Documentation

◆ NodeTypeRole

int QmlObjectTreeModel::NodeTypeRole = Qt::UserRole + 2
staticconstexpr

Definition at line 95 of file QmlObjectTreeModel.h.

Referenced by data(), and roleNames().

◆ SeparatorRole

int QmlObjectTreeModel::SeparatorRole = Qt::UserRole + 3
staticconstexpr

Definition at line 96 of file QmlObjectTreeModel.h.

Referenced by data(), and roleNames().


The documentation for this class was generated from the following files: