adding QGraphicsItem to multiple scenes
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:
Code:
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!
Re: adding QGraphicsItem to multiple scenes
You have to create a second item.
Code:
scene1->addItem(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.
Re: adding QGraphicsItem to multiple scenes
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.
Re: adding QGraphicsItem to multiple scenes
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?
Re: adding QGraphicsItem to multiple scenes
Quote:
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.
Re: adding QGraphicsItem to multiple scenes
Quote:
Originally Posted by
forrestfsu
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.
Re: adding QGraphicsItem to multiple scenes
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:
Code:
{
painter->drawEllipse(10, 10, 10, 10);
}
However, I need to make it unique for each view. I want something like this:
Code:
{
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:
Code:
if (this->ui.view1)
if (this->TheForm::ui.view1)
Re: adding QGraphicsItem to multiple scenes
Got it! This seems to work:
Code:
if (this->objectName() == "view1")
else if (this->objectName() == "view2")