Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:17:50+00:00 2026-06-04T19:17:50+00:00

I am facing a problem which is probably easy to solve but I do

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:17:52+00:00Added an answer on June 4, 2026 at 7:17 pm

    You need to invert the order of statements you perform on your QTransform. The order has to be the inverse of intuitive thinking. This has to do with matrix multiplication of transformation matrices in linear algebra. (Change of basis of the transformation matrix…) The problem is that the transformations apply on the source coordinate system, which is kind of unintuitive.

    So remember: Whenever you want to apply multiple transformations, do them in the inverse order of what you would do with the image.

    For an example see the example code in the documentation of QTransform:

    Scale text to the half width, then rotate it clockwise by 45 degrees, then move its origin to (50, 50):

    enter image description here

    QTransform transform;
    transform.translate(50, 50);
    transform.rotate(45);
    transform.scale(0.5, 1.0);
    

    Another problem is that you should translate the image back to its origin by taking the scaling factor into account (thanks to Mat for pointing this out in the question comment).

    Applied to your code snippet (read from bottom to top for intuitive order of operations):

    // Setup transform
    QTransform trans;
    trans.translate(imgFg.width()*scaleFac/2, imgFg.height()*scaleFac/2);
    trans.rotate(angleDegrees);
    trans.scale(scaleFac, scaleFac);
    trans.translate(-imgFg.width()/2, -imgFg.height()/2);
    

    Last but not least, you want to have the center of your image as the new origin (reference point), so you should not translate back (now the first operation; in your code the last one). Then just paint the image at the reference point rather than adding another size/2. Also, you should not apply the transform to the image and then paint this image; at least it is no good style. Just apply the transform to the painter and un-apply it afterwards.

    QTransform trans;
    trans.scale(scaleFac, scaleFac);
    trans.rotate(angleDegrees);
    trans.translate(imgFg.width()/2,imgFg.height()/2);
    
    // Save old transform (only needed if you use other transformations)
    const QTransform &oldTrans = painter.transform();
    // Apply new transform
    painter.setTransform(trans);
    // Paint image at reference point
    painter.drawImage(referencePoint, imgFg);
    // Restore transform
    painter.setTransform(oldTrans);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Evening, I'm facing a small problem which I can't get fixed. I'm having one
I'm facing a problem which should be really simple to solve, but I am
Im facing following problem I have created mentioned condition, but when I choose y
Currently I am facing a problem which I am unable to solve after a
I'm facing a problem which is probably extremely common in game-design. Let's assume that
I have tried PushMeBaby as server. The problem which I am facing is that
I am facing a problem for which I don't have a clean solution. I
I am facing very strange problem in which I have byte[] and when I
Hay.. I am facing a problem, I am getting a string 'strData' which contain
Facing a problem, it seems my data stored in SQL Server does not stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.