Problems with selection models & views
I am looking for a good/elegant way of making two different objects aware of eachother. Here is what I want to accomplish:
I have a QGraphicsScene that can contain any number of QGraphicsItems, and a QTreeView that lists the names of the QGraphicsItems within the scene in Z order (similar to photoshop's layers box)
I would like to be able to select an item in the scene and have the corresponding line be selected in the layers view, and vice versa.
Right now I have a semi-working solution using setData() to store a pointer to the other item. So the QGraphicsItem stores a pointer to the QStandardItem and vice versa. Here is an abstract of the code:
Code:
int key = 0;
graphicsItem->setData(key, standardItemVariant);
standardItem->setData(graphicsItemVariant);
standardItem->data().value<QGraphicsPixmapItem*>(); //pointer to the graphics item
graphicsItem->data(key).value<QStandardItem *>(); //pointer to the standard item
This seems to work fine when implementing the proper signals & slots, etc.
Except... When I use the above method, my program crashes when I go to internally drag & drop an item within the QTreeView. I've narrowed it down to actually setting the data on the QStandardItem. If I comment out line 10 above, the drag&drop behaviour is as expected (but obviously unable to select the corresponding QGraphicsItem within the scene)
Does anyone have any suggestions for the way I have done it or perhaps a better way to accomplish what I am trying to do? Thanks in advance!
Re: Problems with selection models & views
I think I'm over complicating this... I think I am just going to subclass QStandardItem and QGraphicsPixmapItem and have have private pointers to the corresponding item.
But the issue with setting the data (using setData() ) on the QStandardItem and getting the unexpected internal drag&drop behaviour... Any ideas?
Re: Problems with selection models & views
I think you should subclass QGraphicsItem and emit a signal in mousePressEvent() with the graphics item's unique id as the argument of the signal.
Re: Problems with selection models & views
nimaweb: Thanks, that's an interesting idea. I'll see if I can work up a signal/slot solution...