PDA

View Full Version : Multiple QGraphicsViews with one QGraphicsScene



alisami
12th March 2010, 10:24
Hi,

I am trying to display the items on a graphics scene on two distinct graphics views. My problem is that, I don't want to show all the items on the graphics scene in both views. For example, in my application, on view 1, I will start an editor for the user to draw some items on one view and when the user finishes editing, view 2 will display the newly added item.

Is it possible with one graphics scene?

Sami

aamer4yu
12th March 2010, 10:33
View displays what a scene has. A scene may have multiple views,,, like in 40000 chip demo in Qt Demos..
You can have a editor scene, or even may be a widget. Then from that widget create a QGraphicsItem and add it to mainScene.

alisami
12th March 2010, 16:24
View displays what a scene has. A scene may have multiple views,,, like in 40000 chip demo in Qt Demos..
You can have a editor scene, or even may be a widget. Then from that widget create a QGraphicsItem and add it to mainScene.

But I need to display all the items that are not edited. So if I use an editor scene, I need to add copies of all the items of main scene to the editor scene and that would cost very much.

JovianGhost
22nd March 2010, 04:02
I would do it this way:

- Add a flag to your item which indicates if the item should be rendered in view 2
- Subclass QGraphicsView, and this will be your view 2
- In your new class, reimplement the paintEvent function yourself. In this function, you iterate through each item to draw. If that item has the flag set, call QGraphicsView::paintEvent and pass it the QPaintEvent argument, otherwise do nothing

Now when you add an item, by default the flag is set to false, so it doesn't get rendered in view 2. When you finish drawing the item, set it's flag to true, tell view 2 to update, and you're set!

[edit]
Ok I found another way, while sifting through the docs looking for something else completely.
QGraphicsView has a property called ViewportUpdateMode, which controls how this *particular* QGraphicsView updates itself. If you set this property to QGraphicsView::NoViewportUpdate, then according to the docs,


QGraphicsView will never update its viewport when the scene changes; the user is expected to control all updates. This mode disables all (potentially slow) item visibility testing in QGraphicsView, and is suitable for scenes that either require a fixed frame rate, or where the viewport is otherwise updated externally.

This sounds like what you need. So when you start drawing an item, set view 2's update mode to NoViewportUpdate, and when your drawing finishes, reset the mode to whatever it was before (MinimalViewportUpdate by default) and send an update signal. I haven't tested this so I have no idea if it will work, but you can try.