Usually one implements the following structure:
struct Item {
Item *parent;
QList<Item*> children;
};
struct Item {
Item *parent;
QList<Item*> children;
};
To copy to clipboard, switch view to plain text mode
and then parent() becomes something like:
Item *item = static_cast<Item*>(index.internalPointer());
Item *parent = item->parent;
if(parent
== m_rootItem
) { return QModelIndex();
} // I'm top level Item *grandParent = parent->parent;
int row = grandParent->children.indexOf(parent); // find out the row number of the parent
return createIndex(row, 0, parent);
};
QModelIndex Model::parent(const QModelIndex &index) const {
if(!index.isValid()) return QModelIndex();
Item *item = static_cast<Item*>(index.internalPointer());
Item *parent = item->parent;
if(parent == m_rootItem ) { return QModelIndex(); } // I'm top level
Item *grandParent = parent->parent;
int row = grandParent->children.indexOf(parent); // find out the row number of the parent
return createIndex(row, 0, parent);
};
To copy to clipboard, switch view to plain text mode
This makes sense for trees while it doesn't have to make sense for arbitrary graphs (there is no concept of a root item). And I understood the OP's question correctly and I gave a good answer (to make the tree model a subset of an external graph representation) for it.
Bookmarks