Question
Take a certain tree of QGraphicsItems: a QGraphicsRectItem that has two QGraphicsTextItems as children.
QGraphicsRectItem CompositeObject;
QGraphicsTextItem text1;
QGraphicsTextItem text2;
text1.setParent(CompositeObject);
text2.setParent(CompositeObject);
Now take a QGraphicsScene with two QGraphicsRec开发者_Go百科tItem on a different position:
QgraphicsScene scene;
QGraphicsRectItem* rect1 = scene.addRect(...);
QGraphicsRectItem* rect2 = scene.addRect(...);
rect1.setPos(0,0);
rect2.setPos(0,100);
What I want now is the CompositeObject being a child of rect1 AND rect2; my goal: show the same subtree of QGraphicsItems on two places in the same scene, clipped by the parent rect. Updates on the subtree will be visible on the two places in the QGraphicsView without any manual syncing or such.
Solution which I'm not to sure about
The only thing I can think about on how to do this is: embedding two QGraphicsViews in the scene by using a proxywidget. The subtree I want to see duplicated in my scene will then be a another scene (subScene) on it's own and both QGraphicsViews in my main scene will then show the subScene. This will probably do the trick but introduce a nice overhead because of the proxywidget, which I can't afford since the items in the subScene will have to animate and update regularly.
QGraphicsScene subScene;
QGraphicsTextItem text1;
QGraphicsTextItem text2;
subScene.addItem(text1);
subScene.addItem(text2);
QgraphicsScene scene;
QgraphicsView view1(&subScene);
QgraphicsView view2(&subScene);
QGraphicsProxyWidget* proxy1 = scene.addWidget(&view1);
proxy1.setPos(0,0);
QGraphicsProxyWidget* proxy2 = scene.addWidget(&view2);
proxy2.setPos(0,100);
Are there any other options on how to do this?
You have the right idea of using 2 views on 1 scene. But embedding QGraphicsViews
in a scene can be problematic.
My suggestion is, if you don't care about user interaction directly with the items, to use 2 custom QGraphicsItems
to hold the same QGraphicsScene
that contains the items to be mirrored. In the paint()
of the 2 items, use QGraphicsScene::render()
to create the image and draw it.
Think you can try this:
class QGraphicsProxyItem
: public QGraphicsItem
{
QGraphicsItem* m_item;
public:
QGraphicsProxyItem(QGrahicsItem* item, QGraphicsItem* parent = 0)
: QGraphicsItem(parent)
, m_item(item)
{}
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0)
{
m_item->paint(painter,option,widget);
// also recurcevly draw all child items
}
};
p.s. That code is not complete and not tested but it shows the idea.
精彩评论