Tranforming coordinates of QGraphicsScene
-
In my application I have created custom scene and view by deriving from QGraphicsScene & QGraphicsView.
This is actually an image processing application where I have to use another 3rd party library in which they use different coordinate system.
In Qt there use top left of the pixel is (0, 0)
In my library there use centre of the pixel is (0,0)
So there is a shift of 0.5 px is needed for me to show the contents on the GraphicsScene.I have tried setting transformation for the View but it didn't help.
I have used following code. I have used multiplication by scale because I need to support different zoom levels.CViewWidget::CViewWidget(QWidget *parent) :QGraphicsView(parent) { QTransform trafo = this->transform(); translate((0.5 * trafo.m11()), (0.5 * trafo.m22())); }
If I add a rectange to the scene by using the below code we can see that the top left of the rectangle will be at the top left of the view. But my requirement is to get it draw with an offset of 0.5px in both X & Y axis.
QPen pen = QPen(Qt::green, 0.01); QGraphicsRectItem* rects = this->addRect(0.0, 0.0, 3.0, 3.0, pen);
My requirement is to tranfrorm the scene coordinate with a 0.5 px shift in both X & Y cordinate so that whatever the objects which I add to the scene will be drwan with this offset.
Please let me know how I can achieve this.
-
0,5 px offset isn't much. It will probably only appear in the form of different antialiasing (e.g. instead of a single pixel line with strong green, you see a two-pixel line with a color that mixes green with the surrounding colors, e.g. the background of scene and rectangle).
Have you tried comparing the screenshots with and without the transformation with a suitable graphics software to see such small differences?
-
Please find the below image which shows the current and expected behaviour.
As I have mentioned in my comment if I use that code for translate it doesn't give me what I expected.
-
If you set the translation only in the constructor, changes to your zoom level will not affect it. You will get a translation by a half screen pixel, regardless of your zoom level.