Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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...
    412d8e31-d0c2-4830-a6ce-5b12bd7e89e3-immagine.png

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

    This is the code

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

    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)

    16c59312-0d50-448f-82be-109b74b5a57d-immagine.png

    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(100,100);
    transform.translate(srcImg.width(), srcImg.height() / 2);
    transform.rotate(m_rotation, Qt::YAxis);
    transform.translate(-srcImg.width(), -srcImg.height() / 2);
    
    QPainter painter(this);
    painter.save();
    painter.setTransform(transform);
    painter.drawPixmap(0,0,srcImg);
    painter.restore();
    


  • Thank you so much @Chris-Kawa


Log in to reply