gpt4 book ai didi

Qt图像移动/旋转

转载 作者:行者123 更新时间:2023-12-04 13:08:56 27 4
gpt4 key购买 nike

我只想通过小部件的轴移动图像并围绕小部件的中心旋转(就像任何数字绘画软件中的 Canvas ),但它围绕其左顶点旋转......

QPainter p(this);
QTransform trans;

trans.translate(width()/2, -height()/2);
trans.rotate(angle);

QTransform inverse = trans.inverted();
inverse.translate(-canvas.width()/2, -canvas.height()/2);

p.setTransform(trans);
p.drawImage(inverse.map(canvasPos), canvas);

如何让它正确旋转?

最佳答案

您可以将图像的初始重新居中、旋转和最终结果在小部件中心的居中结合在一个单一的转换中。

QTransform 上的操作以相反的顺序完成,因为最后一个应用于 QTransform 的操作将是第一个应用于图像的操作:

// 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);

关于Qt图像移动/旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com