Results 1 to 3 of 3

Thread: Weird behavior of QGraphicsItem mapFromScene

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Weird behavior of QGraphicsItem mapFromScene

    Hi,

    I have an object in the scene and I use mapFromScene to map a point, which is in the item's scene's coordinate system, to the item's coordinate system.

    When the item’s transformation matrix is identity, and I move the object around the scene and then I use mapFromScene with the current scenePos, the result is always (0,0). However, if I apply to the item a transformation that scales the object to double its width, and I move the object around the scene and then I use mapFromScene with the current scenePos, the result is different at different places of the scene. See for example:

    The object is at the top left of the scene:
    ***************** Start Map pivot ***************
    ScenePos X: 117
    ScenePos Y: 67
    Transformed X: 9.88098e-15
    Transformed Y: 0
    Current transformation Matrix:
    |---|---|---|
    | 2 | 0 | 0 |
    | 0 | 1 | 0 |
    | 0 | 0 | 1 |
    |---|---|---|
    ***************** End map pivot ***************

    The object is at the top right of the scene:
    ***************** Start Map pivot ***************
    ScenePos X: 623
    ScenePos Y: 69
    Transformed X: 2.9976e-15
    Transformed Y: 0
    Current transformation Matrix:
    |---|---|---|
    | 2 | 0 | 0 |
    | 0 | 1 | 0 |
    | 0 | 0 | 1 |
    |---|---|---|
    ***************** End map pivot ***************

    The object is around the center or the scene
    ***************** Start Map pivot ***************
    ScenePos X: 368
    ScenePos Y: 218
    Transformed X: -5.15143e-14
    Transformed Y: 0
    Current transformation Matrix:
    |---|---|---|
    | 2 | 0 | 0 |
    | 0 | 1 | 0 |
    | 0 | 0 | 1 |
    |---|---|---|
    ***************** End map pivot ***************

    Here is the code:
    Qt Code:
    1. void tnkdesignscene::setTransPivotPoint()
    2. {
    3. QPointF refCoordinate;
    4. refCoordinate = SelectedItem->scenePos();
    5. qgraphicsitem_cast<tnkitemcontainer *>(SelectedItem)->setTransPivotPoint(QVector2D(refCoordinate));
    6. }
    7.  
    8. void tnkitemcontainer::setTransPivotPoint(QVector2D vector)
    9. {
    10. qDebug() << "***************** Start Map pivot ***************";
    11.  
    12. qDebug() << "ScenePos X: " << vector.x();
    13. qDebug() << "ScenePos Y: " << vector.y();
    14.  
    15. transPivotPoint = QVector2D(this->mapFromScene(vector.toPointF()));
    16.  
    17. qDebug() << "Transformed X: " << transPivotPoint.x();
    18. qDebug() << "Transformed Y: " << transPivotPoint.y();
    19.  
    20. printMatrix(this->transform());
    21.  
    22. qDebug() << "***************** End map pivot ***************";
    23. }
    To copy to clipboard, switch view to plain text mode 

    Any idea why?

    Thanks,
    Carlos.

  2. #2
    Join Date
    Apr 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Weird behavior of QGraphicsItem mapFromScene

    You usually shouldn't compare two floating numbers for equality. You say that without a transform, your code returns (0,0) but it really returns (0.0, 0.0) i.e. two floats. With a transform, it also returns two floats, and they are close to zero

    Is this really a problem, in the sense that it causes difficulties for a user? If you really need to compare the results, you should compare for equality within a small tolerance, or epsilon. This is much discussed on the web.

  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: Weird behavior of QGraphicsItem mapFromScene

    Any coordinate that is effectively zero (and something e-15 *is* effectively zero), as bootchk points out. There are always small rounding and coordinate conversion errors when transformations are applied to floating point numbers. You're seeing one of them.

Similar Threads

  1. Weird behavior under Ubuntu Gnome
    By psih128 in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2010, 14:26
  2. Replies: 1
    Last Post: 4th May 2010, 18:18
  3. Weird removeRow behavior
    By metalinspired in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 12:42
  4. Weird QMenu behavior on Mac
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2009, 15:18
  5. Weird behavior when readding a window to a QMdiArea
    By seand in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2008, 02:14

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.