Qt旋转/移动自定义项目相对于鼠标

Qt Rotating/moving custom item relative to mousepos

本文关键字:项目 相对于 鼠标 自定义 移动 旋转 Qt      更新时间:2023-10-16

我正在尝试通过鼠标的位置旋转QGraphicsItem。因此,在我的dialog。cpp中,每当鼠标位置发生变化时,我调用这个函数:

void Dialog::rotating()
{
    QPointF local = ui->graphicsView->mapToScene( ui->graphicsView->mouse );
        Player->rotate(local.x(),local.y());
}

ui->graphicsView->setScene(scene);

scene->addItem(Player);
构造函数中的

rotate函数的作用如下:

void player::rotate(int x, int y)
{
    double disX = x - 1100;
    double disY = y - 1300;
    double angle = atan2(disY, disX) * 180 / M_PI;
    setTransformOriginPoint(boundingRect().center());
    setRotation(rotation() + angle - orig);
    orig = angle;
}

,其中(1100,1300)是玩家所在的位置。我很确定这能起作用,但它没有正确旋转。也许是当地的位置不对?

另外,我如何移动播放器,使旋转不影响它?任何意见将不胜感激。提前感谢。

花了我一些时间,但我终于弄明白了!

函数rotate(int,int)的第一行应该是:

double disX = x - (1100 + boundingRect().center().x());
double disY = y - (1300 + boundingRect().center().y());