Results 1 to 8 of 8

Thread: [SOLVED] Painting QGraphicsItem constant size

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default [SOLVED] Painting QGraphicsItem constant size

    I've created a very simple QGraphicsItem (currently just draws a box) that I'd like to draw the same size each time regardless of zoom level.

    What I mean is, if I add two of these items to my scene

    Qt Code:
    1. scene->addItem(new MyCustomItem());
    2. scene->addItem(new MyCustomItem());
    3. scene->items()[0].setPos(-10, 0);
    4. scene->items()[1].setPos(10, 0);
    To copy to clipboard, switch view to plain text mode 

    then start zooming in and out, obviously the item sizes will change depending on zoom level.
    What I'd like to do is keep the item sizes visually the same size, for example, the box would be 10 pixels wide on the user's screen, but the zoom still has an effect, i.e. zooming in will make the items appear farther apart.

    Is there an easy way to do this?
    Last edited by JovianGhost; 20th March 2010 at 19:21.

  2. #2
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QGraphicsItem constant size

    The only way I can see at the moment is in my zoom event handler function, to manually iterate through each item and scale it, thusly:

    Qt Code:
    1. for (int i = 0; i < scene->items().count(); i++)
    2. {
    3. item = scene->items()[i];
    4. if (event->delta() > 0)
    5. item->scale(0.5, 0.5);
    6. else if (event->delta() < 0)
    7. item->scale(2, 2);
    8. }
    To copy to clipboard, switch view to plain text mode 

    But there must be an easier way to do this.
    I was thinking of keeping track of the scale factor somehow, perhaps in the custom item itself, and then in the paint function take this scale factor into consideration in the paint() event.
    But then how would I let each child know that a zoom event occurred?

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting QGraphicsItem constant size

    How about using QGraphicsItem::setFlags(QGraphicsItem::ItemIgnoresTransformations)

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

    JovianGhost (20th March 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Painting QGraphicsItem constant size

    See QGraphicsItem::ItemIgnoresTransformations
    EDIT: Another "use reload before you post"

  6. The following user says thank you to Lykurg for this useful post:

    JovianGhost (20th March 2010)

  7. #5
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QGraphicsItem constant size

    That's PERFECT, thank you!

  8. #6
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QGraphicsItem constant size

    Ach, I take that back, it's not perfect!

    After making this change, when I zoom in, the scrollbars don't appear as they did before! How do I fix this?

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Painting QGraphicsItem constant size

    did you update your scene rect after zooming?

  10. #8
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QGraphicsItem constant size

    Quote Originally Posted by Lykurg View Post
    did you update your scene rect after zooming?
    Yep, that did the trick, thanks! I thought QGraphicsView would do it for me, but I suppose that flag I set prevents that from happening.


    In case anyone stumbles on this thread in the future, here's what I did: after setting the proper scale in the zoom function, I added

    Qt Code:
    1. // Update the scene's bounds
    2. QRectF rect = scene()->itemsBoundingRect();
    3. if (rect.isNull())
    4. scene()->setSceneRect(QRectF(0, 0, 1, 1));
    5. else
    6. scene()->setSceneRect(rect);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 21st March 2010, 03:11
  2. get qgraphicsitem size
    By rogerholmes in forum Newbie
    Replies: 10
    Last Post: 18th February 2010, 05:04
  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. Font size calculation when painting in a QImage
    By Ishark in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 22:22
  5. Force the painting of a QGraphicsItem
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2007, 21:28

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.