Select Item in QTreeView by QGraphicsItem
I have a QAbstractItemModel shown in a QTreeView and i have QGraphicsItems in a QGraphicsScene.
A pointer to a GraphicsItem is stored in the AbstractItemModel so clicking an item in the tree is selecting the associated item in the scene.
I would like to archieve that clicking a Item in the scene will select the associated item in the tree.
The tree is not static - adding and removing items will happen regularly.
I have not found a solution. Assungming that the model consits of several thousand items, workarunds like "testing for equility in loops" etc. are not applicable.
Maybe there is some tricky stuff i have missed?!
QVariant QGraphicsItem::data(int key) const
to stored pointer to Item ?
or internal QHash <graphicsItem, item> ?
I have already a pointer to the node in my GraphicsItem.
The problem is to determine the QModelIndex through the model and the parent-child relationship.
If i have this QModelIndex i could create a QItemSelectionModel and set it in the view.
Having the node gives me the depth of the item in the tree by
looping through the parent till the node is invalid (=root).
But i dont have the indices in the child container. With this indices i could create a QModelIndex determined by parent, row and column.
Storing the index or comparing Qbjects or using QHash will not be applicable concerning the high amount and frequently adding and removing of items.
conceptually, you want to have access to nodes by parent-child in both direction?
The access to the nodes in the abstractitemmodel is not the problem.
A node has the two members
Lets say we have a child in the list at index 3. The child does not know about the index. It just knows about its parent.
Now i could store the index of the child in the node itself. But that would force me to update the indices if a sibling is removed or added.
If an item on pos 2 is removed and it has 4000 sibling i have to update many indices.
But maybe i missed something important ?
In what purpose you want to get index of item?
When you click on it and select the corresponding graphics item?
Clicking a item in the tree to get the graphics item is no problem cause a pointer to the graphics item is part of the userData of the node.
The other way round.
So far i can click the graphicsItem and get the corresponding node in the model through a direct pointer (graphicsItem->Node).
Selecting the item in the VIEW! is the problem.
Or more precisely - creating a QModelIndex in the model without knowing about the row that the item is positioned.
and reassign internal data as Row is slow because so many items.
as cardinal varant - use in memory sqlite
I think using an int as member of a node to determine the index the item has (in the parent's list<node> children) is not the badest solution.
Implementing the increase or decrease of the indices in the model itself. In the methods "insertRows" and "removeRows" i have direct access to the nodes.
So no need to set data through the setData method.
But its kind of ugly. Even having a pointer to a node from outside the model is breaking the interface.
you have to break an egg to make an omelette.
May be you can make some correct of this 'ugly' by reimplementing RESET.
It's slow but...