Results 1 to 7 of 7

Thread: QGraphicsView: Drawing at its particular location again and again while keeping some

  1. #1
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsView: Drawing at its particular location again and again while keeping some

    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

    Qt Code:
    1. setSceneRect(0,-50,50,100)
    To copy to clipboard, switch view to plain text mode 

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

    Is there any way to solve this problem?

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    Eeem.... did you tried simply deleting the "B" object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    Nah..i din.

    Now trying it by

    Qt Code:
    1. //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.
    2. scene->removeItem(scene->itemAt(sceneX+50,sceneY+50));
    3. delete scene->itemAt(sceneX+50,sceneY+50);
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    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.
    Qt Code:
    1. //sqMap is the class used for creation of squares
    2. sqMap square = new sqMap(width, height)
    3. ...
    4. ...
    5. scene->removeItem(square);
    6. delete square;
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. ...
    2. ...
    3. ...
    4. std::vector<sqMap *> sqrBasket;
    5. ...
    6. ...
    7. ...
    8.  
    9. for(int pos=0 ; pos<sqrBasket.size(); pos++)
    10. {
    11. scene->removeItem(sqrBasket[pos]);
    12. delete sqrBasket[pos];
    13. }
    14. for(int j=0; j<=i; j++)
    15. {
    16. tempHeight = array[j] / tempWidth;
    17. [B]square = new sqMap(tempWidth, tempHeight);[/B]
    18. square->setPos(xLocation,yLocation);
    19. int size4toolTip = int(array[j]) * sum / (width * height);
    20. QString toolTip = QString::number(size4toolTip,10);
    21. square->setToolTip(toolTip);
    22. [B] sqrBasket.push_back(square);[/B]
    23. scene->addItem(square);
    24. ...
    25. ...
    26. ...
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView: Drawing at its particular location again and again while keeping s

    Right right...I am actually implementing the Squarified Algorithm. 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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.