Results 1 to 8 of 8

Thread: MapToItem, MapToScene

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default MapToItem, MapToScene

    Hello,

    I am working on a graphics project where as there will parent QGraphicsRectItem. This QGraphicsRectItem will have 3 child QGraphicsEllipseItem. This ellipse needs to be arranged one after another in a row. When I am adding these ellipse item at runtime, its position get offset little bit. This is because I am not using any mapToItem, mapFromItem, mapToScene ot mapFromScene functions. Honestly speaking I am not cleared on the concept of these mapping fuctions. Can anybody please clear my fundamentals on these mapping functions and how I can use the same.



    Thanks


    Manish

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: MapToItem, MapToScene

    You don't need any of these to put child items inside a parent item.

    Each item, no matter what it is, has its own coordinate system. Its coordinate system is used to draw its own contents. Each item is sized and positioned according to the coordinate system of its parent item (or the scene if it is a top-level item).

    So if you have a QGraphicsRectItem with the size (100, 100), then its coordinate system goes from (0,0) to (99, 99). The top left corner is (0,0) and the bottom right corner is (99, 99). If you want to add a QGraphicsEllipseItem as a child, you make the child whatever size you want it to be (in the coordinate system of the parent rectangle), and place it inside the rectangle relative to the top left corner of the rectangle. The (0,0) coordinate for the ellipse is the top left corner of its boundingRect() (the rectangle that encloses the ellipse, not the rectangle you are using for its parent).

    If you want to place an ellipse exactly in the center of the parent QGraphicsRectObject, you have to offset the ellipse's position by the half of the width and half of the height of its boundingRect().

    Edit: Actually, I am not sure if the origin (0,0) of a QGraphicsItem is at the top left or at the center. Someone with a better memory might like to correct me if needed.
    Last edited by d_stranz; 29th January 2015 at 01:58.

  3. #3
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MapToItem, MapToScene

    Thanks d_stranz for detailed update.
    Yes, I understood this concept. What still remains the confusion is when to use mapToParent or mapToScene.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: MapToItem, MapToScene

    You use mapToParent() when you want to translate a position in your child widget into the corresponding position in its parent. You use mapToScene() when you want to translate a position from the child to the corresponding position in the scene.

    For example, if you are holding a piece of paper in your hand, and you make a mark on the paper 10 cm down from the top and 5 cm from the left edge, that is the mark's position in child (paper) coordinates. If that piece of paper is in a big room, you can walk all around the room with the paper, but the child coordinates don't change. However, mapToParent() tells you where the mark is with respect to the room's dimensions (2 m from the North wall and 3 m from the West wall). When you walk around with the paper, the position in the room (i.e. parent coordinates) changes. If your scene is the building where the room is, then mapToScene tells you where the mark is with respect to the walls of the building, and likewise, walking around will change these coordinates too.

    So you use mapToParent when you want to know where something in the child is in the parent's coordinate system, and you use mapToScene when you want to know where it is in the scene.

    But, as is explained in the Qt documentation, a child object's position "pos()" is always in parent coordinates.
    Last edited by d_stranz; 30th January 2015 at 01:21.

  5. #5
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MapToItem, MapToScene

    Thanks a lot d_stranz.
    It is very clear now. Very good explanation.

  6. #6
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: MapToItem, MapToScene

    Hi there, anybody still alive here? I tried really a long time, seems that I won't get it without any help.

    Have a QChart with a QdateTimeaxis, embedded in a qwidget, which itself is centralwidged to a mainwindow.
    A typical QPointF of a drawn QSeries would be p = QPointF(1.51122e+12, 4.1); // strange x-value due to qdatetime->toMsecSinceEpoche(); y-val common decimal value
    Now I'd like to draw some additional information beside that point. I have stored that point separately, so mapFromScene() is already done.

    I really tried all of the mapFrom's and mapTo's. Studied the "callout"-example. Seems that I'm blind on this spot.

    I would wait to receive coordinates like QPoint(355, 277); but I don't. What am I doing wrong? What is the appropriate order of mapTo's and mapFrom's?

    ANY help highly appreciatet.

    Cheers, Lars

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: MapToItem, MapToScene

    You should be using QChart::mapToValue() with the real-world chart coordinates of the location. If your real-world coordinate is QPointF(1.51122e+12, 4.1), that's what you pass to the function. The QPointF that is returned is where you add your new QGraphicsTextItem with the additional information.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: MapToItem, MapToScene

    I'll try a new thread. Anyway, @d_stranz, if you read this: indeed a very good explanation.

Similar Threads

  1. Replies: 1
    Last Post: 13th November 2014, 08:15
  2. Using QGraphicsItem::mapToScene
    By Casper14 in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2014, 21:40
  3. Replies: 0
    Last Post: 14th June 2011, 16:02
  4. mapToItem() question
    By Alex Brophy in forum Newbie
    Replies: 2
    Last Post: 18th April 2008, 09:55

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.