Rotate QImage around Y axis

  • Hi guys,
    I'd like to rotate an image around Y axis of point defined in the bottom right position of the image.

    Something like this...

    I'm playing with QTransform but with strange results...
    Is the QTransform the correct approach?

    This is the code

      QTransform transform;
      //  transform.translate(-960, -960);
      transform.rotate(-m_rotation, Qt::YAxis);
      //  transform.translate(960, 960);
      QPainter painter(this);;
      painter.drawImage(100, 100, srcImg);

    With this code I have some problems:

    • the rotation is around Y axis but on the left image
    • the rotation is made in a perspective view (eg: like this)


    Thank you for any suggestion.

  • Moderators

    In Qt origin is at the top left, not bottom right, and Y axis goes down, not up, so to rotate around the right border you have to move your transform by the width of the image. Since rotation uses perspective you also have to move the origin vertically to the center. Lastly you can't translate by 100,100 at the end. It's all in the order of transformations, so to achieve the effect you want it's something like this:

    QTransform transform;
    transform.translate(srcImg.width(), srcImg.height() / 2);
    transform.rotate(m_rotation, Qt::YAxis);
    transform.translate(-srcImg.width(), -srcImg.height() / 2);
    QPainter painter(this);;

  • Thank you so much @Chris-Kawa

Log in to reply