views:

237

answers:

2

I have an application that displays an editor for a diagram using QGraphicsScene object. I would like to create a read only version of the same dialog but have ability for user to see both at the same time.

SimScene* pScene1 = new SimScene(model);  // adds model to scene
SimScene* pScene2 = new SimScene(model);  // adds model to scene
QGraphicsView* pView1 = new QGraphicsView();
pView1->setScene(pScene2);
QGraphicsView* pView1 = new QGraphicsView();
pView2->setScene(pScene2);

When I create 2 instances of QGraphicsScene and use addItem on the second one it removes all the items from the first one. Does Qt support any sort of sharing of model between scenes? Is my only choice to have same scene and try to customize the view? Later one doesn't seem to work because object selection information is within the graphics items being shared so if I disable flags on them they become read only in both views. Any advice is appreciated. Thanks.

A: 

I think that you're storing QSceneItems in model classes. Because of that pScene1 and pScene2 are trying to share not only the model itself, but also the scene items. This won't work because any scene item can be placed only on one scene at any given moment.

How to fix it? Make model not aware of any GUI. Let it issue changed() notifications whenever something interesting happens.

Then let each SimScene wrap model into whatever QSceneItems it wants, and process changed() notifications.

Example:

Model: Graph, Edge, Vertex
GUI SimScene, QEdge, QVertex, QSimInfo, Qbackground, and so on ...

wuub
You are right. The existing system has model items as GUI items and that's why they end up being removed from the first scene upon addition to the second. I will try separating them. Thanks.
Alex Achinfiev
+1  A: 

If you just want an interactive and a read-only view on your model you can use a single QGraphicsScene and 2 QGraphicsViews. You just have to call QGraphicsView::setInteractive(false) on one of them. That way you don't have to change any item flags.