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

#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, QByteArrayroleNames () const override
 
QObject * getObject (const QModelIndex &index) const
 Returns the QObject* stored at index, or nullptr if invalid.
 
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.
 
QModelIndex appendItem (QObject *object, const QModelIndex &parentIndex, const QString &nodeType)
 Same as above but also sets a nodeType tag on the created tree node.
 
QModelIndex insertItem (int row, QObject *object, const QModelIndex &parentIndex=QModelIndex())
 Inserts object at row under parentIndex. Returns the new item's index.
 
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.
 
QObject * removeItem (const QModelIndex &index)
 
int childCount (const QModelIndex &parentIndex=QModelIndex()) const
 Number of direct children under parentIndex.
 
QModelIndex childIndex (int row, const QModelIndex &parentIndex=QModelIndex()) const
 Returns the QModelIndex for the child at row under parentIndex.
 
QModelIndex indexForObject (QObject *object) const
 Searches the entire tree for object and returns its QModelIndex (invalid if not found)
 
QModelIndex parentIndex (const QModelIndex &index) const
 Convenience wrapper around parent()
 
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
 
int count READ count NOTIFY countChanged (bool dirty READ dirty WRITE setDirty NOTIFY dirtyChanged) 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
 

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 25 of file QmlObjectTreeModel.h.

Constructor & Destructor Documentation

◆ QmlObjectTreeModel()

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

Definition at line 60 of file QmlObjectTreeModel.cc.

◆ ~QmlObjectTreeModel()

QmlObjectTreeModel::~QmlObjectTreeModel ( )
override

Definition at line 65 of file QmlObjectTreeModel.cc.

Member Function Documentation

◆ appendChild()

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

Definition at line 358 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 276 of file QmlObjectTreeModel.cc.

References index(), 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 221 of file QmlObjectTreeModel.cc.

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

Referenced by appendChild(), and appendRootItem().

◆ appendRootItem()

void QmlObjectTreeModel::appendRootItem ( QObject *  object)

Definition at line 353 of file QmlObjectTreeModel.cc.

References appendItem().

◆ childCount()

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

Number of direct children under parentIndex.

Definition at line 74 of file QmlObjectTreeModel.h.

Referenced by removeChildren().

◆ childIndex()

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

Returns the QModelIndex for the child at row under parentIndex.

Definition at line 77 of file QmlObjectTreeModel.h.

◆ clear()

void QmlObjectTreeModel::clear ( )
overridevirtual

◆ clearAndDeleteContents()

void QmlObjectTreeModel::clearAndDeleteContents ( )

Clears the tree and calls deleteLater on every QObject.

Definition at line 415 of file QmlObjectTreeModel.cc.

References ObjectItemModelBase::beginResetModel(), and ObjectItemModelBase::endResetModel().

◆ columnCount()

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

Definition at line 110 of file QmlObjectTreeModel.cc.

References index(), and parent().

◆ contains()

bool QmlObjectTreeModel::contains ( QObject *  object) const

Definition at line 428 of file QmlObjectTreeModel.cc.

◆ count()

int QmlObjectTreeModel::count ( ) const
overridevirtual

Implements ObjectItemModelBase.

Definition at line 194 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 334 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 211 of file QmlObjectTreeModel.cc.

References index().

◆ hasChildren()

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

Definition at line 116 of file QmlObjectTreeModel.cc.

References parent().

◆ index()

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

◆ indexForObject()

QModelIndex QmlObjectTreeModel::indexForObject ( QObject *  object) const

Searches the entire tree for object and returns its QModelIndex (invalid if not found)

Definition at line 324 of file QmlObjectTreeModel.cc.

References index().

◆ 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 237 of file QmlObjectTreeModel.cc.

References ObjectItemModelBase::_resetModelNestingCount, ObjectItemModelBase::_signalCountChangedIfNotNested(), index(), 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 232 of file QmlObjectTreeModel.cc.

References index(), 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 171 of file QmlObjectTreeModel.cc.

References index().

◆ parent()

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

Definition at line 90 of file QmlObjectTreeModel.cc.

References index().

Referenced by columnCount(), hasChildren(), index(), and rowCount().

◆ parentIndex()

QModelIndex QmlObjectTreeModel::parentIndex ( const QModelIndex index) const
inline

Convenience wrapper around parent()

Definition at line 83 of file QmlObjectTreeModel.h.

Referenced by appendChild(), appendItem(), appendItem(), insertItem(), insertItem(), removeAt(), and removeChildren().

◆ removeAt()

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

Definition at line 363 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 368 of file QmlObjectTreeModel.cc.

References ObjectItemModelBase::_resetModelNestingCount, ObjectItemModelBase::_signalCountChangedIfNotNested(), childCount(), index(), 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 286 of file QmlObjectTreeModel.cc.

References ObjectItemModelBase::_signalCountChangedIfNotNested(), index(), and setDirty().

Referenced by removeAt().

◆ removeRows()

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

Definition at line 177 of file QmlObjectTreeModel.cc.

References index().

◆ roleNames()

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

Definition at line 183 of file QmlObjectTreeModel.cc.

References index(), NodeTypeRole, and ObjectItemModelBase::roleNames().

◆ rowCount()

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

Definition at line 104 of file QmlObjectTreeModel.cc.

References parent().

◆ setData()

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

Definition at line 145 of file QmlObjectTreeModel.cc.

References index(), and ObjectItemModelBase::ObjectRole.

◆ setDirty()

void QmlObjectTreeModel::setDirty ( bool  dirty)
overridevirtual

Member Data Documentation

◆ NodeTypeRole

constexpr int QmlObjectTreeModel::NodeTypeRole = Qt::UserRole + 2
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: