Results 1 to 2 of 2

Thread: resizing and moving a QGraphicsItem

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default resizing and moving a QGraphicsItem

    Hello,

    I have a QGraphicsPathItem that I create after 'drawing' with the mouse on a scene. I have a background image in the scene and this path item gets created on top of the image which is almost transparent reflects a selection of an area of the underlying image. So basically I have my scene with two items.

    Now when I press the zoom button I don't actually do and transformations to my scene or view, I simply compute the appropriate image somewhere else and then replace the image to the zoomed one. But now I need the drawn image also to take into account the zoom level and when I scroll to also scroll accordingly. I was trying to do this getting the boundingRect of the item and calculate a new bounding rect according to zoom and scroll position and then setting that calculated rectangle to be the new bounding rectangle of my item, so my item would resize itself and move to the right position whether I zoom or scroll. But this is not the right way since it gives very strange behavior. I do all the prepareForGeometryChange() before setting the boundign rectangle which I set in a function like this:


    Qt Code:
    1. QRectF PathItem::boundingRect() const
    2. {
    3. return m_rect;
    4. }
    5.  
    6. void PathItem::setBoundingRect(QRectF rect)
    7. {
    8. prepareGeometryChange();
    9. m_rect = rect;
    10. update(m_rect);
    11. }
    To copy to clipboard, switch view to plain text mode 



    What would be the right way to do this? Basically how can I have an item in the scene move and rescale itself to a pre-calculated rectangle I provide it? Any hints and tips greatly appreciated.

    Thanks.

  2. #2
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: resizing and moving a QGraphicsItem

    Well so far found out that when I set the bounding rectangle, it does change it to whatever I set it. I create my path Item drawing a path on the scene and then creating the item giving that path. So when I set it a new bounding rectangle I want the same exact path to be redrawn within the new bounding rectangle that I gave it. So far no matter what size of rectangle I set it to the item disappears, but I noticed that the outline of the bounding rectangle is there, just that the actual path item is not redrawn in the new bounding rectangle.
    I keep the QPainterPath that I give it as a member variable so I am doing this:

    Qt Code:
    1. void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QBrush brush(QColor(200, 200, 200));
    4. painter->setBrush(brush);
    5. painter->setOpacity(0.4);
    6.  
    7. m_path.setFillRule(Qt::WindingFill);
    8. painter->drawPath(m_path);
    9. }
    10.  
    11. QRectF PathItem::boundingRect() const
    12. {
    13. return m_rect;
    14. }
    15.  
    16. QPainterPath PathItem::shape() const
    17. {
    18. return m_path;
    19. }
    20.  
    21. void PathItem::setPath(const QPainterPath &_path)
    22. {
    23. QGraphicsPathItem::setPath(_path);
    24. m_path = _path;
    25. m_rect = path().boundingRect();
    26. }
    27.  
    28. void PathItem::setBoundingRect(QRectF rect)
    29. {
    30. prepareGeometryChange();
    31. m_rect = rect;
    32. update(m_rect);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Where am I going wrong?

Similar Threads

  1. Replies: 3
    Last Post: 10th June 2014, 20:48
  2. Moving of QGraphicsItem
    By Erlendhg in forum Qt Programming
    Replies: 17
    Last Post: 5th November 2013, 15:04
  3. Moving child QGraphicsItem
    By roband915 in forum Qt Programming
    Replies: 0
    Last Post: 27th February 2011, 12:19
  4. Moving QGraphicsItem with mouse
    By BrainFreeze in forum Qt Programming
    Replies: 9
    Last Post: 11th February 2011, 00:48
  5. Update scene after moving QGraphicsItem
    By rogerholmes in forum Newbie
    Replies: 1
    Last Post: 19th January 2010, 05:08

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.