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?!
best regards
-
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.
-
The access to the nodes in the abstractitemmodel is not the problem.
An Example:
A node has the two members
Node parent;
QList<Node> children;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 ?
-
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. -
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.
But ...
you have to break an egg to make an omelette.