Qt image move/rotation

前端 未结 2 1093
一向
一向 2021-01-20 05:25

I just want to move the image by the widget\'s axises and rotate around the widget\'s center(like a canvas in any digital painting software), but it\'s rotating around its l

相关标签:
2条回答
  • 2021-01-20 05:51

    You can combine the initial recentering of the image, the rotation and the centering of the final result at the widget center in a single transformation.

    The operations on QTransform are done in the reverse order, because the lastest one applied to QTransform would be the first one applied to the image :

    // QImage canvas;
    QPainter p(this);
    QTransform trans; 
    
    // Move to the center of the widget
    trans.translate(width()/2, height()/2);
    
    // Do the rotation
    trans.rotate(angle); 
    
    // Move to the center of the image
    trans.translate(-canvas.width()/2, -canvas.height()/2); 
    
    p.setTransform(trans);
    // Draw the image at (0,0), because everything is already handled by the transformation
    p.drawImage(QPoint(0,0), canvas);
    
    0 讨论(0)
  • 2021-01-20 05:51

    The usual reason an object rotates around its top left point, rather than its centre, is because it has its dimensions defined with 0,0 at the top left, rather than being in the centre of the object.

    You haven't shown what the 'canvas' object is, so assuming it's something like a QGraphicsRectItem, you'd need to declare its top left, width, height as -x/2, -y/2, width, height to ensure that the centre point of the object is at 0,0. Then when you rotate the object, it will rotate about its centre.

    Additionally, you should try to separate the rotation and translation logic from the painting functions for optimum performance.

    0 讨论(0)
提交回复
热议问题