Given 4 points being be the result of
QPolygon poly = transform.mapToPolygon(QRectF(0, 0, 1, 1));
how can I find QTransform transform
? (Even better: also given an arbitrary source rectangle)
Motivation: Given the four corner points of an image to be drawn in a perspectively distorted coordinate system, how can I draw the image using QPainter?
This is a screenshot illustrating the problem in GIMP, where one can transform a layer by moving around the 4 corners of the layer. This results in a perspective transformation. I want to do exactly the same in a Qt application. I know that QTransform is not restricted to affine transformations but can also handle perspective transformations.
You should be able to do this with QTransform.squareToQuad
. Just pass it the QPolygonF
you want to transform to.
I've sometimes had some issues getting squareToQuad to do what I want, and have had to use QTransform.quadToQuad
instead, defining my own starting quad, but you might have more luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With