forked from qt-creator/qt-creator
TreeModel: Add a sibling() implementation
The base implementation uses the parent() index, which in turn accesses the grand parent level. We can save a few cycles here. Change-Id: I3c3696118fe7c3d4563ee3cd28981fca62013828 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -966,6 +966,18 @@ QModelIndex BaseTreeModel::parent(const QModelIndex &idx) const
|
|||||||
return createIndex(i, 0, static_cast<void*>(parent));
|
return createIndex(i, 0, static_cast<void*>(parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QModelIndex BaseTreeModel::sibling(int row, int column, const QModelIndex &idx) const
|
||||||
|
{
|
||||||
|
const TreeItem *item = itemForIndex(idx);
|
||||||
|
QTC_ASSERT(item, return QModelIndex());
|
||||||
|
QModelIndex result;
|
||||||
|
if (TreeItem *parent = item->parent()) {
|
||||||
|
if (TreeItem *sibl = parent->childAt(row))
|
||||||
|
result = createIndex(row, column, static_cast<void*>(sibl));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int BaseTreeModel::rowCount(const QModelIndex &idx) const
|
int BaseTreeModel::rowCount(const QModelIndex &idx) const
|
||||||
{
|
{
|
||||||
CHECK_INDEX(idx);
|
CHECK_INDEX(idx);
|
||||||
|
|||||||
@@ -188,6 +188,7 @@ protected:
|
|||||||
QVariant data(const QModelIndex &idx, int role) const override;
|
QVariant data(const QModelIndex &idx, int role) const override;
|
||||||
QModelIndex index(int, int, const QModelIndex &idx = QModelIndex()) const override;
|
QModelIndex index(int, int, const QModelIndex &idx = QModelIndex()) const override;
|
||||||
QModelIndex parent(const QModelIndex &idx) const override;
|
QModelIndex parent(const QModelIndex &idx) const override;
|
||||||
|
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
|
||||||
Qt::ItemFlags flags(const QModelIndex &idx) const override;
|
Qt::ItemFlags flags(const QModelIndex &idx) const override;
|
||||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||||
bool hasChildren(const QModelIndex &idx) const override;
|
bool hasChildren(const QModelIndex &idx) const override;
|
||||||
|
|||||||
Reference in New Issue
Block a user