PDA

View Full Version : QGraphicsView: Drawing at its particular location again and again while keeping some



qtzcute
16th July 2009, 11:18
Hi :)

I am facing a problem in drawing at two different location of same GraphicsView widget.

My Graphics View geometery is let say (0,0,100,100)

i set my scene rectangle to (-50,-50,100,100). Then i draw a rectangle (say A) in left half of the Graphics View Widget. After this i draw a rectangle (say B) in right half of the Widget. Now i don't like this rectangle B and i want to remove and draw another rectangle (say B*) in place of it "while keeping the rectangle A untouched".

Is there anyway by which i can like 'lock' the rectangle A so even if now i create new object of my scene by 'newGraphicsScene', for the creation of B*, my rectangle A may not vanish.

I tried setting rect to right half of the GraphicsView Widget by


setSceneRect(0,-50,50,100)

but this vanished my rectangle A, which i did not want.

Is there any way to solve this problem?

thanks

wysota
16th July 2009, 11:28
Eeem.... did you tried simply deleting the "B" object?

qtzcute
16th July 2009, 14:13
Nah..i din.

Now trying it by



//sceneX and sceneY are the top-left co-ordinates of my scene and my rectangles //(that i want to remove) has the top-left cornor same as scene rectangle.
scene->removeItem(scene->itemAt(sceneX+50,sceneY+50));
delete scene->itemAt(sceneX+50,sceneY+50);


This sometime removes the item but sometimes when i run my program again without making any changes in my code, that rectangle appear on top of other newly designed shapes

wysota
16th July 2009, 14:49
Adressing an item by its expected coordinates is a bad idea. You should have a pointer to it and simpy delete it. You can either keep the pointer around or retrieve it upon clicking an item or something based on the event coordinates (i.e. extracted from the mouse event object).

qtzcute
19th July 2009, 17:44
ahaan...

I removed the item i did not want and delete the pointer which was pointing to it by the following code. And my program then worked fine.


//sqMap is the class used for creation of squares
sqMap square = new sqMap(width, height)
...
...
scene->removeItem(square);
delete square;


But now i need to delete multiple last drawn items. So what i did was that i made a vector of type *sqMap and then pushed the items i wanted to delete into it and then removed and deleted them one by one. But some/all of the items don't get removed from the screen. Following code was used



...
...
...
std::vector<sqMap *> sqrBasket;
...
...
...

for(int pos=0 ; pos<sqrBasket.size(); pos++)
{
scene->removeItem(sqrBasket[pos]);
delete sqrBasket[pos];
}
for(int j=0; j<=i; j++)
{
tempHeight = array[j] / tempWidth;
square = new sqMap(tempWidth, tempHeight);
square->setPos(xLocation,yLocation);
int size4toolTip = int(array[j]) * sum / (width * height);
QString toolTip = QString::number(size4toolTip,10);
square->setToolTip(toolTip);
sqrBasket.push_back(square);
scene->addItem(square);
...
...
...


Does the proedure of removing mulitple items that i have followed is right?

wysota
19th July 2009, 21:20
You can use whatever means you want to store pointers to items. The code you presented is incomplete so it is hard to say whether it is ok or not. If you always delete last x items, consider using QStack.

qtzcute
20th July 2009, 05:48
Right right...I am actually implementing the Squarified Algorithm (http://www.codeproject.com/KB/recipes/treemaps.aspx). I din show the whole code because i thought you won't have much time to go through it and understanding other's code is sometimes difficult, but may be not to a Guru :)

I hope this QStack solve my problem. If not, then i will surely turn to you for help. :)

Thanks