I am facing a problem which is probably easy to solve but I do not get it right:
I have two QImages, a fixed-size background image imgBg
, and a second QImage imgFg
which should be transformed using affine transformations and then painted (and clipped) on the background image.
The affine transformation is as follows:
- Scale image around its origin (uniform scaling)
- Rotate image around its origin
- Translate the foreground image to a certain reference position on the background image so that the foreground's image center is on the reference position.
Here is what I have done so far:
// Setup transform
QTransform trans;
trans.translate(-imgFg.width()/2, -imgFg.height()/2); // move center of image to origin
trans.scale(scaleFac, scaleFac);
trans.rotate(angleDegrees);
trans.translate(imgFg.width()/2, imgFg.height()/2);
// Transform foreground image
imgFg = imgFg.transformed(trans);
// Paint on background image
QPointF referencePos(imgBg.width()/3, imgBg.height()/2); // some reference position
QPainter painter(&imgBg);
painter.drawImage(referencePos - QPointF(imgFg.width()/2, imgFg.height()/2), imgFg);
As long as I only change the reference position everything works fine. When using scaling and/or rotation the image is also correctly scaled and rotated but it is not placed on the correct position (the foreground's image center does not match the reference point). Is there something wrong with the transformation pipeline? Do I have made a mistake?