Results 1 to 8 of 8

Thread: How to implement a shape resizer with QGraphicsPolygonItem?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to implement a shape resizer with QGraphicsPolygonItem?

    Maybe I don't understand your issue. I thought you were trying to implement resizing of graphics items using resize handles on the bounding rectangles of the items.

    What you implemented in your initial post works fine for rectangles and ellipses because they are defined by their bounding rectangles. Polygons and other items are defined by an arbitrary closed set of points connected by lines, but they do have a bounding rectangle, the smallest rectangle that contains all of the points. QPolygon (on which QGraphicsPolygonItem is based) has a constructor that takes a QRect argument, but that creates a rectangle polygon. That is the problem you ran into when you tried to implement the same code for resizing polygons that you used for the rectangle and ellipse.

    The Wikipedia article I pointed you to shows how to construct a scale transformation using homogenous coordinates, which is what QTransform uses. Your resize code needs to look something like this (and of course you will add checks to avoid divide by zero):

    Qt Code:
    1. float sx = float( rect.width() ) / float( boundingRect.width() );
    2. float sy = float( rect.height() ) / float( boundingRect.height() );
    3.  
    4. QTransform transform;
    5. transform.scale( sx, sy );
    6.  
    7. QPolygonF oldPolygon = item->polygon();
    8. QPolygonF newPolygon = oldPolygon * transform;
    9.  
    10. item->setPolygon( newPolygon );
    To copy to clipboard, switch view to plain text mode 

    This code will change the aspect ratio. If you want to keep the same aspect ratio, then you will need to change one of the scale factors so that the new width and height preserve the same ratio as the old width and height.

    But like I said earlier, the scale transformation scales the object around its (0,0) point. If the polygon's origin (0,0) is not at the center of the polygon, then scaling will probably also move it. If that happens to you, you will have to scale in three steps: translate your polygon to (0,0), scale, translate back. But by default, QGraphicsItem coordinates have the origin at their center (except for QGraphicsTextItem, QGraphicsPixmapItem, and maybe a few others that have their origin at topLeft).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    xd123 (16th April 2020)

  3. #2
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to implement a shape resizer with QGraphicsPolygonItem?

    Thank you so much. I now completely see what you meant, I'm sorry for not understanding it the first time :D working with Qt for just about 4 months now. I implemented your idea and it works just as I wanted it to. Really appreciate your help and willingness to explain your solution.

    If anybody stumbles on this issue too here is the working polygon resizer class:
    Qt Code:
    1. class PolygonResizer : public SizeGripItem::Resizer // resize class for polygon
    2. {
    3. public:
    4. virtual void operator()(QGraphicsItem* item, const QRectF& rect)
    5. {
    6. QGraphicsPolygonItem* polygonItem =
    7. dynamic_cast<QGraphicsPolygonItem*>(item);
    8.  
    9. QRectF itemBoundingRect = item->boundingRect();
    10. float sx = float( rect.width() ) / float( itemBoundingRect.width() );
    11. float sy = float( rect.height() ) / float( itemBoundingRect.height() );
    12.  
    13. QTransform transform;
    14. transform.scale( sx, sy );
    15.  
    16. QPolygonF oldPolygon = polygonItem->polygon();
    17. QPolygonF newPolygon = oldPolygon * transform;
    18.  
    19. if (polygonItem)
    20. {
    21. polygonItem->setPolygon( newPolygon );
    22. }
    23. }
    24. };
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to xd123 for this useful post:

    d_stranz (17th April 2020)

  5. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to implement a shape resizer with QGraphicsPolygonItem?

    Happy to help. A couple of suggestions:

    - Since itemBoundingRect and rect are already QRectF (which is floating point based), there is no need to cast width() and height() to float. They already are. I was assuming they were QRect, which is integer based and thus needed casting to avoid rounding errors on division. (if either height or width of the rect is less than the height or width of the bounding rect, integer division would always yield zero as the scale factor).

    - You should move the check on polygonItem validity to right after the dynamic_cast. If it is null, then retrieving the oldPolygon a few lines later will blow up.

    - You should also use qgraphicsitem_cast<> instead of dynamic_cast<> in all of your resizing methods.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. resizer view item does not work properly
    By paule22 in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2015, 18:44
  2. QGraphicsPolygonItem Points
    By dacrawler in forum Newbie
    Replies: 1
    Last Post: 23rd April 2012, 05:59
  3. QGraphicsPolygonItem with holes
    By natnan in forum Qt Programming
    Replies: 0
    Last Post: 2nd August 2010, 09:44
  4. I need help in QGraphicsPolygonItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2009, 10:53
  5. QPaintEngine and QGraphicsPolygonItem
    By magland in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 16:03

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.