Home > QGraphicsScene, Qt > On The Coordinate System Of QGraphicsScene In Qt

On The Coordinate System Of QGraphicsScene In Qt

When it comes to Qt’s fancy 2D engine embedded in QGraphicsScene – usually attached to a QGraphicsView – a common question is what the coordinate system of it is like. Basically this means how the X and Y axes of its grid are oriented. The official Qt documentation is of little use here as they probably never consider this to be important, let alone for exchanging data with other coordinate systems, such as that of OpenGL.

For a 2.5D Arcade game my company Nyanko is currently working on I had to create a level editor. I chose Qt and QGraphicsScene for the simple reason that it is very easy to set up a kind of editor inside a fancy UI with it. When it comes to interoperability with our in-house 3D engine which is based upon OpenGL, the question was how compatible these two coordinate systems of Qt and OpenGL would be. As it turned out they’re almost the same, but also different. See the below illustrations:

opengl_coordinate_system
OpenGL’s coordinate system

qt_coordinate_system
QGraphicsScene’s coordinate system

As you can see, QGraphicsScene has an inverted Y axis in comparison to OpenGL, with the Y axis growing downwards instead of upwards. This makes the starting point of OpenGL bottom-left and that of QGraphicsScene top-left. It’s unfortunate that this orientation was chosen, as in the level editor I had to invert the Y axis prior to saving to a level file and again upon loading. It also means that the OpenGL functionality in Qt doesn’t have the smooth interoperability with QGraphicsScene it otherwise would have had.

Fortunately it’s not hard to invert the Y axis, it’s just a shame that it had to be done like this, and with so little documentation. Hopefully this article will help someone out there avoid a few pitfalls 🙂

 

Maya

Advertisements
  1. Lucas Tanure
    September 1, 2013 at 4:48 PM

    Hi,

    But how did you do it ?
    Thanks

    • September 1, 2013 at 4:51 PM

      Hi Lucas, what are you referring to specifically?

      • Lucas Tanure
        October 6, 2013 at 5:32 PM

        I have one pixmap image in my qgraphicscene and a few points for that image. So I add my image to qgraphicscene, and later I add the points. But every point gets the wrong position because they were taken in the normal coordinate system, (0,0) bottom left, not up left.

        So how do I set the coordinate system of scene to Y growing to up in the screen ? And the images that I add doesn’t get up side down ?

  2. erelender
    September 6, 2013 at 11:50 AM

    Hi Maya,

    I have worked with Qt and OpenGL and found the same issue a little bit annoying, but i got around it by setting a transform to my view, which scales the y coordinates with -1. That way you wouldn’t need to fiddle with your coordinates when saving/loading. You still need to do a little mapping with mouse events though.

    It may be a little late for this comment but i found out about your blog recently via your post on QThread (and i have to say as someone who has been using Qt for 5 years, your post made me truly understand QThreads.)

    • September 7, 2013 at 1:36 PM

      Hi erelender,

      Setting a scaling factor of -1 is indeed another good way get around it. I’d say it depends on the application what is the most convenient/practical method.

      Glad you enjoyed the QThreads article as well 🙂

  3. March 26, 2015 at 7:44 AM

    Hi, Maya,

    Setting a scaling factor of (1, -1) could invert the y, but when display QGraphicsTextItem, the text is also be inverted.

    • May 29, 2015 at 8:15 PM

      Yes, everything would be inverted. One can however invert text and such prior to adding it to the scene. The reversal would then make it the right way around again.

  4. February 14, 2016 at 8:43 PM

    Hello, I used this thinking and I noticed that my QGraphicsScene is set to have (0,0) on the top left corner. Is there a way to set it to be like you show?

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: