Results 1 to 4 of 4

Thread: QGraphicsItem coordinate systems

  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 QGraphicsItem coordinate systems

    I am having a miserable time trying to understand how coordinate systems work in the Graphics / View architecture. I've read everything I can find, but none of the examples seem to apply to my situation.

    For discussion purposes, assume I have objects that I want to represent as QGraphicsItem instances. These items have world-based coordinates in centimeters. I want to draw them (in the QGraphicsItem :: paint method) using their centimeter dimensions.

    I want to put them in a scene that has dimensions in meters (or feet, or miles, or anything else except cm). For argument's sake, say the scene has a scene rect 3 m square.

    So if I have an object that is a square 100 cm x 100 cm, in its paint() method I call

    Qt Code:
    1. painter->drawRect( QRectF( 0, 0, 100, 100 ) );
    To copy to clipboard, switch view to plain text mode 

    In my view, I get a rectangle 100 *pixels* square, no matter what I try to do with transforms, scaling, or anything else in the scene to map from item to scene coordinates.

    Every example I have seen appears to use pixel coordinates everywhere (view, scene, items), with no examples on how to properly use transformations.

    Can anyone point me to source code for a non-trivial Graphics / View application that uses world coordinates for items, with proper mapping to scene and view? I am thinking that something like a CAD or scientific plotting application would show me what I need to do.

    Thanks in advance for any help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem coordinate systems

    The default scene-view scale is 1:1 thus one logical unit in scene coordinates corresponds to one pixel in the view coordinates. If you want to change that, you need to "zoom" the view on the scene using QGraphicsView::scale() or similar. It's similar between the scene and the item. If you want an item 100cm wide and the scene is scaled in metres then you need to scale down the item 100 times so that 1 logical unit of item coordinates becomes 1/100 logical units of scene coordinates. If I'm not clear enough then feel free to ask for further explanations.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #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: QGraphicsItem coordinate systems

    The default scene-view scale is 1:1 thus one logical unit in scene coordinates corresponds to one pixel in the view coordinates.
    Yes, got that. I actually just figured out what I was doing wrong.

    Qt Code:
    1. scene->setRect( 0, 0, 200, 200 );
    2.  
    3. // MyGraphicsItem constructor sets initial bounding rect to (0, 0, 1000, 100)
    4. MyGraphicsItem * item = new MyGraphicsItem();
    5.  
    6. item->setPos( 10, 10 );
    7.  
    8. // Scale x coordinate only (y coordinate *is* pixels)
    9. QGraphicsScale scale;
    10. scale.setXScale( scene->width() / item->boundingRect().width() );
    11.  
    12. QList< QGraphicsTransform * > transforms;
    13. transforms.push_back( &scale );
    14. item->setTransformations( transforms );
    15.  
    16. // ...
    To copy to clipboard, switch view to plain text mode 

    I determined that my mistake was that the QGraphicsScale instance can't be created on the stack (or if it is, its lifetime must be at least the same as the QGraphicsItem that uses it). Changing the QGraphicsScale instance to a heap-allocated pointer fixes the problem, and I now get a properly scaled line.

    So, I think I'm on my way after 3 days of trying to understand and getting a bloody forehead from pounding it on the desk.

    One additional question: Can QGraphicsTransform instances be shared among several items, or are they "owned" by the item after calling setTransformations()?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem coordinate systems

    I'd have to look into Qt's code. But it is certainly safer to have a 1:1 mapping between the transform and the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    d_stranz (15th June 2011)

Similar Threads

  1. Tray icon is missing on some systems.
    By eurodatar in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2011, 00:39
  2. Safety critical systems and qt
    By yagabey in forum Qt Programming
    Replies: 2
    Last Post: 8th April 2009, 14:11
  3. BSD Systems
    By Brandybuck in forum General Discussion
    Replies: 8
    Last Post: 28th May 2008, 15:36
  4. how do i use a same project in multiple systems ?
    By nhatkhang in forum KDE Forum
    Replies: 6
    Last Post: 21st December 2006, 16:32
  5. Systems color support
    By kemp in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 10:18

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.