Results 1 to 5 of 5

Thread: Resizing QGraphicsItems [solved]

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Resizing QGraphicsItems [solved]

    Hi,

    I'm writing a simple Viso like diagramming program, and I'm having some trouble with QGraphicsView not updating properly when things are resized. At its simplest, I have a rectangular QGraphicsItem, and when the user drags the resize grip in its corner they can resize the rectangle. The relevant methods are below:

    Qt Code:
    1. void Sticky::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    2. if(sizeGripItem->contains(mapToItem(sizeGripItem, event->pos()))) {
    3. resizing = true;
    4. }
    5.  
    6. QGraphicsItem::mousePressEvent(event);
    7. }
    8.  
    9. void Sticky::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    10. if(resizing) {
    11. setSize(QSize(event->pos().x(), event->pos().y()));
    12. emit(moved(this));
    13. update();
    14. }
    15. else {
    16. emit(moved(this));
    17. QGraphicsItem::mouseMoveEvent(event);
    18. }
    19. }
    20.  
    21. void Sticky::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
    22. if(resizing) {
    23. rect.setBottomRight(graphWidget->getNearestGridPoint(rect.bottomRight()));
    24. relayout();
    25. emit(moved(this));
    26. // hack to tell the canvas we're at a new size
    27. moveBy(1,1);
    28. moveBy(-1,-1);
    29.  
    30. scene()->update();
    31. }
    32. resizing = false;
    33. QGraphicsItem::mouseReleaseEvent(event);
    34. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, on mouse click, I set a resizing flag, then on mouse move, I set the new size of the component by changing its bounding rectangle. This code has two problems:
    Firstly, if I quickly resize the box smaller, artifacts are left on the screen where the box was. So when I set the new size of the boundingrect, the view doesnt refresh properly.
    Secondly, when I am done resizing, for some reason the scene doesn't know about the new size of the item, so mouse clicks won't work properly.

    These two problems both boil down to the same issue I think, which is that the scene is never informed that my item has changed, so it cannot do things like repaint properly, or update the sizes for mouse click detection.

    As you can see in my mouseReleaseEvent, I use the hack of moving the item by one pixel, and moving it back to tell the scene that I've updated the size of the item, but this is definitely not optimal.

    Any ideas about how to resize items properly?
    Last edited by pherthyl; 18th February 2007 at 02:35.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Resizing QGraphicsItems

    Quote Originally Posted by pherthyl View Post
    Any ideas about how to resize items properly
    Try what's suggested in this post.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    elsheikhmh (8th April 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing QGraphicsItems

    Thank you so much. Works great!

  5. #4
    Join Date
    Apr 2007
    Posts
    12
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Re: Resizing QGraphicsItems [solved]

    Quote Originally Posted by pherthyl View Post
    At its simplest, I have a rectangular QGraphicsItem, and when the user drags the resize grip in its corner they can resize the rectangle.
    @pherthyl: how can you place resize grip on you custom subclassed QGraphicsItem?

    I'm so sorry for hacking this thread. but I'm overwhelmed.

    Thanks,
    - Mustafa

  6. #5
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resizing QGraphicsItems

    Quote Originally Posted by jpn View Post
    Try what's suggested in this post.
    This link doesn't exist anymore. Does anyone knows how to do it?

Similar Threads

  1. Performance problems with overlapping qgraphicsitems
    By brjames in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2008, 21:42
  2. form not auto resizing
    By quickNitin in forum Newbie
    Replies: 5
    Last Post: 7th June 2007, 10:00
  3. Resizing QGraphicsItem
    By philentropist in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2007, 08:28
  4. QTabWidget - problem with resizing
    By moowy in forum Qt Programming
    Replies: 5
    Last Post: 14th September 2006, 14:06
  5. Prevent from resizing a QMainWindow
    By Flier in forum Qt Tools
    Replies: 5
    Last Post: 14th April 2006, 17:11

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.