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:
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
QGraphicsPixmapItem *graphicsItem = new QGraphicsPixmapItem();
QStandardItem *standardItem = new QStandardItem();
QVariant graphicsItemVariant = QVariant::fromValue(graphicsItem);
QVariant standardItemVariant = QVariant::fromValue(standardItem);
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
To copy to clipboard, switch view to plain text mode
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!
Bookmarks