Results 1 to 4 of 4

Thread: Scale independent QGraphicsItem

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Scale independent QGraphicsItem

    I'm trying to represent a map using a QGraphicsScene. I've set up a custom view, and implemented zooming functionality. So far, I've gotten basic polygons and lines working but have come across a snag when trying to represent points or labels.

    Since there is no "point item" in the API, I'm using QGraphicsEllipseItem to represent points on the map. My problem is that these are effected by the scale, so they completely disappear if I zoom out very far. This also occurs for any text labels I place in the scene.

    Is there a way to make custom graphics items that always draw with a fixed size? I want to avoid specifically drawing anything in the view since I may have multiple views into the scene (although I may consider putting text labels there).


    Update: I've come up with a solution for drawing points, but I'm not sure if this is the best method. This sets a minimum size when drawing on the screen. As such, the point will never disappear from the view when zoomed out.

    Note: This example code hasn't been optimized for drawing speed, since each point would be required to calculate the drawing scale.

    Qt Code:
    1. #pragma once
    2. #include <QGraphicsItem>
    3.  
    4. class GraphicsPointItem : public QGraphicsItem
    5. {
    6. private:
    7. static qreal sRadius;
    8. static qreal sDiameter;
    9.  
    10. public:
    11. QRectF boundingRect() const
    12. {
    13. return QRectF(-sRadius, -sRadius, sDiameter, sDiameter);
    14. }
    15.  
    16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    17. {
    18. QRectF objectRect = boundingRect();
    19. QRectF mappedRect = painter->transform().mapRect(objectRect);
    20.  
    21. qreal widthRatio = objectRect.width() / mappedRect.width();
    22.  
    23. if (widthRatio > 1) // Stop the point from shrinking after zooming out
    24. painter->scale(widthRatio, objectRect.height() / mappedRect.height());
    25.  
    26. QBrush brush(Qt::black);
    27. painter->setBrush(brush);
    28. painter->setPen(Qt::NoPen);
    29.  
    30. painter->drawEllipse(-sRadius, -sRadius, sDiameter, sDiameter);
    31. }
    32. };
    33.  
    34. qreal GraphicsPointItem::sDiameter = 4;
    35. qreal GraphicsPointItem::sRadius = 2;
    To copy to clipboard, switch view to plain text mode 
    Last edited by Twoslick; 5th March 2010 at 21:54. Reason: Update

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Scale independent QGraphicsItem

    May I ask why in your boundingRect() method, you use negative co-ordinates? I'm new to Qt and quite lost on little things like that.

  3. #3
    Join Date
    Mar 2010
    Location
    Kawasaki, Japan
    Posts
    13
    Qt products
    Qt/Embedded Qt Jambi
    Platforms
    Symbian S60

    Default Re: Scale independent QGraphicsItem

    Hi,

    You seem to be scaling your QGraphicsEllipseItem also in the paint().
    Please see if you can avoid scaling of the points and labels - depending on your requirement of when you want to show the points and labels.

    If my understanding is correct, then the points and labels would have to scale up when your map is scaled-up.

    Regards
    Girish

  4. #4
    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: Scale independent QGraphicsItem

    Did you come across QGraphicsItem::ItemIgnoresTransformations
    May be thats what you need for your ellipse item

Similar Threads

  1. Platform independent GUI
    By bnilsson in forum Qt Programming
    Replies: 39
    Last Post: 28th November 2009, 11:00
  2. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 00:18
  3. Replies: 11
    Last Post: 6th March 2009, 22:15
  4. Independent Right-Click Menus
    By HiFly in forum Qt Programming
    Replies: 5
    Last Post: 20th February 2009, 07:58
  5. QGraphicsItem::scale() bug or not ?
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2007, 15:44

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.