PDA

View Full Version : adding QGraphicsItem to multiple scenes



forrestfsu
3rd November 2006, 18:00
My problem is as follows...I have a QGraphicsItem that I would like to have in more then one scene. So I add the QGraphicsItem to scene1, then I add the same QGraphicsItem to scene2. The code would look something like this:


scene1->addItem(theQGraphicsItem);
scene2->addItem(theQGraphicsItem);

The problem is that the QGraphicsItem will only show up in scene2. I know that addItem removes the graphicsItem from any previous scene before adding it to the new one, but is there any way to make it add it to both?

Thanks!

wysota
3rd November 2006, 18:16
You have to create a second item.

QGraphicsItem *item1 = new MyGraphicsItem;
scene1->addItem(item1);
QGraphicsItem *item2 = new MyGraphicsItem(*item1);
scene2->addItem(item2);
Note, that this requires a copy constructor. If your item doesn't have one, simply create the second item with the same parameters as the first one.

forrestfsu
3rd November 2006, 20:15
Thanks Wysota,

I was hoping I didn't have to do that considering the amount of items i had, but I guess that's the main/simplest solution.

wysota
3rd November 2006, 21:29
It's the only solution. Each item keeps its scene dependent state in its object, so you can't share an object between multiple scenes. Do you need multiple scenes at all? Maybe a single scene with multiple views will be enough?

forrestfsu
3rd November 2006, 22:19
Do you need multiple scenes at all? Maybe a single scene with multiple views will be enough?
I was trying to think of a way to use a single scene, but I couldn't come up with a solution. Maybe if I give you a background of what I'm doing, you can give me some feedback.

I have three seperate graphics views which need to do the following:
1) Show a mosaic of approx 20 selectable QGraphicsItemGroup objects
2) When the user selects 1 of the 20 images from GraphicsViewNum1, it should show that selected QGraphicsItemGroup with an altered background (this might sound a bit confusing)...let me know if I need to clarify.
3.) When the user clicked GraphicsViewNum2, GraphicsViewNum3 should show that selected QGraphicsItemGroup zoomed in.

So my problem has been, if I use only one scene and I add approx 20 QGraphicsItemGroup's to it. How can I make it so that I only show one of the QGraphicsItemGroup's in graphicsView2, using only 1 scene? I haven't been able to come up with a solution, so I simply created multiple scenes with duplicate QGraphicsItemGroup's for each scene.

wysota
3rd November 2006, 22:48
So my problem has been, if I use only one scene and I add approx 20 QGraphicsItemGroup's to it. How can I make it so that I only show one of the QGraphicsItemGroup's in graphicsView2, using only 1 scene?

I think you can use QGraphicsView::setSceneRect() to ask the view to only visualise a rectangle occupied by a single group.

forrestfsu
6th November 2006, 15:08
I've been attempting a different solution for part of my problem. Perhaps the following can work? To display the same QGraphicsItemGroup on different views with a different background, can I reimplement:

QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)

I was able to get it to display a background on all the views:


void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->drawEllipse(10, 10, 10, 10);
}

However, I need to make it unique for each view. I want something like this:


void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
if (this->ui.view1)
painter->drawEllipse(10, 10, 10, 10);
else if (this->ui.view2)
painter->drawEllipse(20, 20, 20, 20);
else if (this->ui.view3)
painter->drawEllipse(30, 30, 30, 30);
}


However, I can't seem to figure out how to code the if statement to recognize a specific graphics view on my form...any tips? I've tried:



if (this->ui.view1)
if (this->TheForm::ui.view1)

forrestfsu
6th November 2006, 15:25
Got it! This seems to work:



if (this->objectName() == "view1")
else if (this->objectName() == "view2")