The few times I have dealt with this problem, I have beat my head against the wall for a few days trying to figure out the parent-child relationships, and have ended up implementing an internal tree data structure which maps the external model into the tree needed by QTreeView. The tree node corresponding to the row/column in the view is stored as a pointer in the QModelIndex created in the index() method.
Nodes in the tree can be as simple as
struct TreeNode
{
TreeNode * parent;
QVector< TreeNode * > children;
MyModelElement * pointerToModelForThisNode;
}
struct TreeNode
{
TreeNode * parent;
QVector< TreeNode * > children;
MyModelElement * pointerToModelForThisNode;
}
To copy to clipboard, switch view to plain text mode
and your QAbstractItemModel simply stores a pointer to the root of the tree, then traverses it as appropriate to create QModelIndex values for parent and child relationships in the QTreeView.
Bookmarks