Link a QTreeView to items in a QGraphicsScene
-
This is not a code question, rather it's about how to use some Qt objects together. I hope it's appropriate here.
I've got a
QTreeView
filled with items that inherit fromQStandardItem
andQGraphicsItem
so that they can be added both to aQStandardItemModel
and aQGraphicsScene
.I'm now implementing signals for selection. I want the tree item to be selected when the graphics item is selected, and vice versa. It should also work for multiple selections, i.e. multiple clicked items in the scene should select multiple list items, and vice versa.
I started to use selection signals to do this, but then I found out about
QItemSelectionModel
and I wondered if this was a better way to do this. It seems to allow list items to easily toggled/selected.I have no experience with
QItemSelectionModel
. I'm not quite sure how it is supposed to be used. For example, Is it supposed to be subclassed, and the class then used to handle the signal events? Or does it work some other way? If someone has an example of code that does something similar to what I'm trying to do (link items between a tree and a scene) then I'd be happy to see them! -
Hi
I dont think you need to subclass it.
Simply uses it with indexes to sync the selection of scene items with the
Tree.https://doc.qt.io/qt-5/model-view-programming.html#handling-selections-in-item-views
(see the code samples here for how to select something)If you combine the TreeView with
https://doc.qt.io/qt-5/qpersistentmodelindex.html#detailsi think you can simply store the related index in the QGraphicsItem and use the/a
QItemSelectionModel and its select function to handle having one or more selected items.
That said , i have not tried this and there might be gotchas :)