PDA

View Full Version : automatic coordinate transformation for a data plot in QGraphicsView



rubenvb
25th September 2009, 14:26
Dear all,

I'm trying to write a simple QWidget class that draws the data in a QPolygonF (=QVector<QPointF>). What I am trying to do is create a QTransform that holds the appropriate coordinate transformation info to transform from a real cartesian coordinate system to the pixel integer coordinate system. The purpose of this is so that I can use from within my QWidget:


QPainter p(this);
p.setWorldTransform( QTransform( /*appropriate values*/ ) );
p.drawLine( /*coordinates in real/double values);


This should be possible, but my calculated transformation does not work as intended :(.
I have tried this with following assumption:
drawLine converts the parameters by multiplying the values with the matrix, which leads to the (rounded off) integer values used to draw the line in the viewed area.
Then the resize operation would only require adapting the matrix to the new height and width and repainting the scene.

This does not happen :(, could someone with more knowledge of QGraphicsview help please. Thanks!

wysota
26th September 2009, 10:17
Could you prepare a minimal compilable example reproducing the problem?

rubenvb
26th September 2009, 16:08
Here goes...http://www.mediafire.com/?sharekey=e3ceb3cc8f829b8b1f8e0fff488e27e038fe691d 7472ed3ab8eada0a1ae8665a

I would like to be able to plot a cartesian function, like y=x², by using a QPainter::drawLine( QpointF, QPointF ) and having the transformation between real coordinates and integer coordinates take place within the class, fully automatic through the transformation matrix.

The transformation formulas I'm using are:
x' = x/X
y' = Y - y/Y

with x' and y' being integer coordinates (with (0,0) being in the topleft corner and the vertical axis downward), x and y being real cartesion coordinates( with (0,0) in the bottomleft corner and the y axis upward), X and Y the integer width and height respectively of the widget (to accommodate resizing and automatic repainting later on).

The code I uploaded doesn't draw a line from the bottom-left corner right and up. When the matrix isn't set (setWorldTransform is commented out), it of course just draws a silly line, but this is not what I'm trying to accomplish.

wysota
27th September 2009, 00:50
Here goes...http://www.mediafire.com/?sharekey=e3ceb3cc8f829b8b1f8e0fff488e27e038fe691d 7472ed3ab8eada0a1ae8665a
Please use the forum's attachment feature instead of relying on 3rd party sites to link code.

Well... the "test" project doesn't actually reproduce or describe any problem... From your description it seems that you want a non-linear transformation of physical and logical coordinates. A 3x3 matrix transformation will not allow you to do that.

rubenvb
27th September 2009, 11:55
My bad, sorry. Here it is.

wysota
27th September 2009, 13:55
I can only see an empty window in your project.

BTW. Remember drawLine() takes four integers. If you want to use fraction values, you have to use another variant of drawLine() (like the one that takes a QLineF parameter).