Results 1 to 5 of 5

Thread: Resizing QGraphicsItems [solved]

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.