Results 1 to 6 of 6

Thread: QGraphicsItem scale problem

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QGraphicsItem scale problem

    Hi,

    I am writing a graphical application that require scale of graphic items. In the application the user drags and drops polygon from a toolbar (like Dia does). When the polygon lands into the scene it has an standard size. I need to implement the scale of the items. I'm using the setScale function. The problem that I have happens when the user want to scale an already scaled item. For example if I scale an item to 2.0 the result is a item twice its original size, if I do 2.0 again the function uses the original polygon and not the scaled one resulting in the same size.

    I tried to work around the problem with the following code:
    Qt Code:
    1. //Scaled polygon
    2. QPolygonF spoly;
    3.  
    4. //One polygon in the scene
    5. tnkPolygonSymbol *item;
    6.  
    7. //Gets one polygon from the scene
    8. item = qgraphicsitem_cast<tnkPolygonSymbol *>(scenes[currentPage]->items()[0]);
    9.  
    10. //Scale the item to double
    11. item->setScale(2.0);
    12.  
    13. //Gets the scaled polygon from the scene
    14. spoly = item->mapToScene(item->polygon());
    15.  
    16. //Restores the polygon to the original size
    17. item->setScale(1.0);
    18.  
    19. //Replaces the current polygon with the scaled polygon
    20. item->setPolygon(spoly);
    To copy to clipboard, switch view to plain text mode 

    However this does not work. The result of the process is shown in this image:


    After the process, the bounding rectangle is double its original size but the resized polygon is away from its bounding rectangle. This may happen because the coordinate values of the spoly are not the same as the item in the scene. I tried to set the coordinates of spoly with moveTo, CeterOn, etc and nothing seems to work.

    Any ideas will be appreciated.

    Thanks,
    Carlos.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsItem scale problem

    I think you're looking too far.

    setScale(scale() * magnification)

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem scale problem

    Hi,

    Sorry but I don't understand your reply.

    I'm using setScale to set the scale but how can I scale an already scaled object? For example to scale a polygon to twice it size and then to a scale that mirrors it? The result should be a mirror of twice the original size. I can do this by recording the progressive scales given to an item but in the event of saving the scene to a file I would need to save such progression in the scale so that I can recreated it when opening the file again. A better idea would be by swapping the original object by its scaled version: Original size -> Scale -> new original size -> Scale -> new original size -> ........ So I would need just to save the final version of the item.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsItem scale problem

    Well, if I understand you correctly, you want this:

    Original object, scale factor = 1
    First scale, double the size -> scale factor = 2
    Second scale, double the size -> scale factor = 4
    etc..

    Then use:
    setScale(scale() * magnification) where magnification = 2 for doubling

  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem scale problem

    ok. It make sense.

    And how can I adjust the bounding rectangle to match the rescaled object? The application behaves like InkScape. When selecting an item a set of drag points will appear around the bounding rectangle of the item so the user can change its scale in an arbitrary form.

  6. #6
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QGraphicsItem scale problem

    I'm replying to my own post.

    The problem is solved with the following code:
    Qt Code:
    1. //Scaled polygon
    2. QPolygonF spoly;
    3. //Current polygon
    4. QPolygonF cpoly;
    5. //One polygon in the scene
    6. tnkPolygonSymbol *item;
    7. //Gets one polygon from the scene
    8. item = qgraphicsitem_cast<tnkPolygonSymbol *>(scenes[currentPage]->items()[0]);
    9. //Gets the current polygon from the item
    10. cpoly = item->polygon();
    11. //Transformation matrix
    12. QTranform trans;
    13. //Transform the matrix to an scale of double it size;
    14. trans = trans.scale(2.0,2.0);
    15. //Scale the current polygon to the scaled polygon using the tranformation matrix
    16. spoly = trans.map(cpoly)
    17. //Replaces the current polygon with the scaled polygon
    18. item->setPolygon(spoly);
    To copy to clipboard, switch view to plain text mode 
    With this the item will have the current scaled polygon and the bounding rectangle will adapt to it.

    Thanks for your replies.
    Carlos.

Similar Threads

  1. Scale independent QGraphicsItem
    By Twoslick in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2010, 05:59
  2. Scale division problem
    By Indalo in forum Qwt
    Replies: 0
    Last Post: 2nd December 2009, 08:36
  3. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 00:18
  4. Replies: 11
    Last Post: 6th March 2009, 22:15
  5. QGraphicsItem::scale() bug or not ?
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2007, 15:44

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.