Search code examples
c++qtqgraphicsviewqgraphicsscene

Qt4: QGraphicsScene/View and custom transformation method?


I know that it is possible to use affine transformations with Qt. But is it also possible to set a complete custom global transformation method?

Use case: Drawing projected geographic points (lat, long) or retrieve mouse events etc. in geographic corrdinates (in the context of a QGraphicsScene/View).

At the moment I use it like that (little bit pseudo code):

MyPoint pt = myProjection(geographicPoint);
QPoint(pt.x, pt.y);

// or, to make it shorter, but essentially it's the same
QPoint p  = myProjection(geoPoint);

geoPoint = myBackProjection(mouseEvent.getPoint());

And I'd like to do "register" my transformation methods somewhere so that the QGraphicsView (or whoever is responsible) internally uses these methods before it draws something on the screen.

Or does that neither make sense (because it would introduce problems where I don't expect them, e.g. in calculating distances) nor is it possible?


Solution

  • QGraphicsView uses a QTransform matrix, which is a 3x3 matrix. Therefore you can only do linear transformations. In Qt5/QtQuick or with QtQuick3d you may be able to achieve this with a QML ShaderProgram.

    In "pure" Qt4 you would have to derive your own custom class from QGraphicsView, or QAbstractScrollArea or QGraphicsScene and override the paint methods.