Results 1 to 15 of 15

Thread: Qt Coordinate System and the Graphics View Framework

  1. #1
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt Coordinate System and the Graphics View Framework

    What is the best way to use the Qt Coordinate system with the Graphics View Framework?
    (I am using Qt 4.3.0)

    At the moment I just scale the view to (1, -1), but this is not enough if you add text to the scene, since the text gets mirrored. The solution is to additionally mirror the text, but this can get complicated in some situations and it is probably a performance issue when you have a lot of items on the scene.
    But I think that there is another problem. If I would print this scene, would it be mirrored?

    Is it better to leave the coordinate system as it is and just multiply the y coordinate and the angle with -1 when you get (set) the location and orientation from (of) the item?
    This can probably be solved with the lower level of the API, so it probably wouldn't even be noticed on the higher level of the API. Is this better?

    Does anyone have a better suggestion?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    But what are you trying to do?
    Scaling? Or what?

    Please be more detailed in what exactly are you trying to achieve.

    Regards

  3. #3
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    I am making an EDA program (schematic, PCB...) and I would like to make a "normal" Cartesian coordinate system, so the user wouldn't get confused with it. This is why I mirror the view ( scale(1, -1) ). But as I said the problem is with text items, since the text also gets mirrored...

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    Well, if the problem is just with text items, why don't you just map them back to Qt's normal space?
    You could subclass QGraphicsTextItem and have it done automatically, when it is created, moved, etc...

    Regards

  5. #5
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    At the moment I do something similar. I have a class that has a QGraphicsTextItem and I mirror/rotate it when needed.

    What about printing the scene to image/file? Will it get mirrored? (I have some other work before I start implementing printing)

    Is it better to use the coordinate system as it is and just move (y) and rotate items in the opposite direction?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    I don't think you have to rotate.
    You just have to translate the X axis with (sceneHeight). The Cartesian coordinate system is not asymmetric to the one Qt uses. It is just shifted on the oY axis.
    So, you always must add a certain height to your item's position.

    About printing: just apply the same mapping on the painter and you will get the same results.

    Regards

  7. #7
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    Quote Originally Posted by marcel View Post
    You just have to translate the X axis with (sceneHeight).
    You mean the Y axis?

    Quote Originally Posted by marcel View Post
    The Cartesian coordinate system is not asymmetric to the one Qt uses. It is just shifted on the oY axis.
    Did you mean inverted (multiplied with -1)?


    What would happen if I would use scale(1, -1) on the view and the textItem? Would it be displayed correctly? (have to try it soon...)

    What about performance issue with all this scaling? Schematics can have from about 10 to 100 symbols and a lot of nets (lines) that connect the symbols, PCB are usually even bigger. Can this be a problem?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    You mean the Y axis?
    No, I meant the X axis, visually.
    Which means inverting the y component.

    Did you mean inverted (multiplied with -1)?
    Yes, exactly. It should be OK if you apply a scale of (1, -1).

    What about performance issue with all this scaling? Schematics can have from about 10 to 100 symbols and a lot of nets (lines) that connect the symbols, PCB are usually even bigger. Can this be a problem?
    I am not sure about that, because it depends on the complexity of the items.
    It actually all comes down to repainting the view.
    There are a few recent posts regarding graphics view performance. Maybe those can be of some help.

    Regards

  9. #9
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    Quote Originally Posted by djurodrljaca View Post
    What would happen if I would use scale(1, -1) on the view and the textItem? Would it be displayed correctly? (have to try it soon...)
    Well this solves the problem. Now all I have to do is use scale(1, -1) on all QGraphicsItems and on the QGraphicsView and I get the real Cartesian coordinate system...

    If you knew how much time I spent figuring out what transformation to use and all I had to do is a simple scale(1, -1)...

    (...bangs his head against the wall really really hard...)

  10. #10
    Join Date
    Jul 2007
    Location
    Slovenia
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Coordinate System and the Graphics View Framework

    Quote Originally Posted by djurodrljaca View Post
    Well this solves the problem. Now all I have to do is use scale(1, -1) on all QGraphicsItems and on the QGraphicsView and I get the real Cartesian coordinate system...
    I was wrong again. All shape items are OK, since they are "immune" to "mirroring", the only problem are the items that include text. In this items you only have to mirror the text.
    ("immune" means that the shape is at the right location and rotation only when you scale the view)

    To sum things up:
    All I have to do is use scale(1, -1) on all QGraphicsTextItems (and QGraphicsSimpleTextItems) and on the QGraphicsView and then you get the real Cartesian coordinate system...

    I hope this helps someone...

  11. #11
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt Coordinate System and the Graphics View Framework

    Quote Originally Posted by djurodrljaca View Post
    I hope this helps someone...
    Damn, I spent almost a month getting the coordinate system right and Im looking at this post now!
    (...bangs his head TOO against the wall really really hard...)

    BUT, I have one more HUGE problem.
    I am working on a map utility, similar to Google Maps (Not that extensive though.) I have used scale(1,-1) for getting the coordinate system right, just the way djurodrljaca has done. I am using picture (svg files) to represent cities and other prominent places on the map. When I draw these pictures using QGraphicsPixmapItem, I have explicitly used pixmapItem->scale(1,-1); to bring it back to normal. BUT now, when I zoom on to the view, the picture is also zoomable, i.e. it gets bigger and bigger, something that is undesirable. So I used the qt flag QGraphicsItem::ItemIgnoresTransformations. But then the result is just not visible. And if I change the implementation a little, it remains constant in size regardless of the zoom level, but is again mirrored!
    Similarly, I am using QGraphicsTextItem for showing labels for cities on the map and when I use this flag, QGraphicsItem::ItemIgnoresTransformations, on it, with scale(1,-1), the result is just the same...

    Please suggest something. I have already spent a lot of time getting this thing right and I am still nowhere near it

  12. #12
    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: Qt Coordinate System and the Graphics View Framework

    Did you try setting an inverted rectangle (starting from negative vertical values and ending at 0 or starting at positive vertical value and ending at 0 or negative) as the scene rect? This way you wouldn't have to scale anything.

  13. #13
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt Coordinate System and the Graphics View Framework

    Hey, thanks for that reply. N my apologies for replying back late ...
    Well, if I invert the scene rectangle, all my points, lines and polygon shapes drawn on it get inverted too. By scaling the view scale(1,-1) I get what I want, like showing proper coordinates on the screen, except only for QGraphicsPixmapItem and QGraphicsTextItem (They get drawn inverted).
    The kludge that I adopted worked for me. I transformed the pixmap used in QGraphicsPixmapItem using QTransform. So now it is shown properly onto the canvas. BUT, if i use QGraphicsItem::ItemIgnoresTransformations flag (To keep the picture from changin when the zoom level is changed), the picture just disappears! I dont know why and I have not been able to solve this issue as well...

    Is there something wrong with the coordinate system (s) set for scene, view and/or the items in Qt???

  14. #14

    Default Re: Qt Coordinate System and the Graphics View Framework

    has anyone managed to figure out if there is a good way to rotate/translate the coordinate system so that when drawing angles, zero degrees is up and value increase clockwise instead of zero degrees to the right and values increase counter-clockwise?

  15. #15
    Join Date
    Feb 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: Qt Coordinate System and the Graphics View Framework

    I have found the solution.
    Use a QGraphicsItemGroup to contain the text items. Use the group to set coords, scales and rotation. The text items ignores the transformations.
    For example:

    Qt Code:
    1. grp->addToGroup(text);
    2. text->setFont(font);
    3. text->setPos(0,0);
    4. text->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    5. grp->setPos(x,y);
    To copy to clipboard, switch view to plain text mode 


    Pay attention that the text item must be added to group before his setting his coordinates, since coordinates of the sub-item are relative to the parent item, BUT when adding the item to the group his coords relative to the scene are preserved.

    Hope that helps.

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.