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
    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 

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

    d_stranz (17th April 2020)

  3. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

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