Results 1 to 2 of 2

Thread: QGraphicsView coordinates system problem

  1. #1
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default QGraphicsView coordinates system problem

    Hi,

    This is a great site, and I am glad to have found it!
    I hope someone could help me with the following problem:

    I am trying to figure out how QGraphicsView coordinate system works, based on the docs, and it doesn't match up to what I see as a result of the code I write.
    Here are the points that I don't understand, and below is the code for my example that shows the problems.
    I will try to post the resulting screenshot for that code.

    The first point is, item coordinates.
    The docs state that:
    Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations.
    and
    At item's position is the coordinate of the item's center point in its parent's coordinate system; sometimes referred to as parent coordinates. The scene is in this sense regarded as all parent-less items' "parent". Top level items' position are in scene coordinates.
    However, if I add an item to a parent item, and do setPos(0,0) the added item appears at the top left corner, instead of being at the middle as the docs say.
    (As can be seen in the screen shot)

    Another issue is the position of a top level item in the scene.
    In my code I set a sceneRect(), so the scene should NOT be unlimited.
    In addition, if I add an Item to it, and do setPos(0,0) the item is neither at the top left, nor is it in the middle.
    It seems that the scene shows more then its sceneRect().
    If I ask the position of the item, it returns correct (0,0) but visually, it is at some distance from the rims of the scene.
    If I want a scene which has its left upper corner as 0,0 and bottom right corner as 500,500, what should I do?
    setSceneRect() is not giving that result (as can be seen in screen shot.)

    The last question is about the scene rect.
    Since I am setting the scene rect, I expect the scene to have a fixed size.
    However, if I resize my view, the scene resizes to fill it, even though the setSceneRect() was set.

    So what am I missing here?

    Here is my code.
    It creates a basic set of QGraphicView elements.
    I am placing 2 QGraphicsRectItems.
    The red one (rect1) is the top level item, the blue one (rect2) is a child item of rect1.
    I gave the scene a green background to see if it resizes in the view, and it does.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QHBoxLayout layout;
    5. w.setLayout(&layout);
    6. w.setGeometry(0,0,500,500);
    7.  
    8. scene.setBackgroundBrush(QBrush(Qt::green));
    9. scene.setSceneRect(0,0,450,450);
    10.  
    11. QGraphicsView view(&scene,&w);
    12.  
    13. QGraphicsRectItem rect1(0,0,100,100);
    14. rect1.setBrush(QBrush(Qt::red));
    15.  
    16. QGraphicsRectItem rect2(0,0,50,50,&rect1);
    17. rect2.setBrush(QBrush(Qt::blue));
    18.  
    19. //Place items in scene
    20. scene.addItem(&rect1);
    21. rect1.setPos(0,0);
    22. rect2.setPos(0,0);
    23.  
    24. //Put it all togeather
    25. layout.addWidget(&view);
    26. w.show();
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    qgraphicsview..png

    Thanks!

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 40 Times in 39 Posts

    Default Re: QGraphicsView coordinates system problem

    Quote Originally Posted by Oximoron View Post
    Hi,
    However, if I add an item to a parent item, and do setPos(0,0) the added item appears at the top left corner, instead of being at the middle as the docs say.
    (As can be seen in the screen shot)
    The key word here is usually. When you pass a rectangle, you pass its origin with it.

    Qt Code:
    1. // This is a centered rectangle:
    2. QGraphicsRectItem rect1(-50,-50,100,100);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Oximoron View Post
    Another issue is the position of a top level item in the scene.
    In my code I set a sceneRect(), so the scene should NOT be unlimited.
    In addition, if I add an Item to it, and do setPos(0,0) the item is neither at the top left, nor is it in the middle.
    It seems that the scene shows more then its sceneRect().
    If I ask the position of the item, it returns correct (0,0) but visually, it is at some distance from the rims of the scene.
    If I want a scene which has its left upper corner as 0,0 and bottom right corner as 500,500, what should I do?
    setSceneRect() is not giving that result (as can be seen in screen shot.)
    Use this class to show you the true extents of the scene rect:
    Qt Code:
    1. struct GraphicsScene : public QGraphicsScene
    2. {
    3. void drawBackground(QPainter *painter, const QRectF &rect)
    4. {
    5. painter->fillRect(sceneRect()&rect, backgroundBrush());
    6. }
    7. };
    To copy to clipboard, switch view to plain text mode 
    The scene rect isn't as hard a constraint as you might think. You can even move items outside of the scene rect, and they will still be rendered if the QGraphicsView is big enough.


    Quote Originally Posted by Oximoron View Post
    The last question is about the scene rect.
    Since I am setting the scene rect, I expect the scene to have a fixed size.
    However, if I resize my view, the scene resizes to fill it, even though the setSceneRect() was set.

    So what am I missing here?
    I hope it is clear now that the scene isn't resizing. If you want your QGraphicsView to show the scene and nothing more.
    you could call setMaximumSize() on it.

  3. The following user says thank you to spud for this useful post:

    Oximoron (27th April 2010)

Similar Threads

  1. Replies: 10
    Last Post: 9th June 2016, 17:02
  2. Replies: 4
    Last Post: 18th March 2010, 10:11
  3. Problem with QGraphicsView/Scene coordinates
    By Andrewshkovskii in forum Newbie
    Replies: 12
    Last Post: 19th October 2009, 10:41
  4. Replies: 1
    Last Post: 9th April 2009, 14:54
  5. QGraphicsView coordinates transformation
    By mtribaldos in forum Newbie
    Replies: 1
    Last Post: 6th February 2008, 17: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
  •  
Qt is a trademark of The Qt Company.