【问题标题】:Qt Rotating/moving custom item relative to mouseposQt相对于mousepos旋转/移动自定义项目
【发布时间】:2014-06-22 20:50:11
【问题描述】:

我正在尝试按鼠标的位置旋转 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);

在构造函数中。

调用的旋转函数是这样做的:

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;
}

where (1100, 1300) 是玩家所在的位置。 我很确定这可以解决问题,但它不能正确旋转。也许本地位置不正确?

另外,我如何移动播放器以不影响旋转?任何输入将不胜感激。提前致谢。

【问题讨论】:

    标签: c++ qt rotation qgraphicsitem


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-27
      • 2013-05-02
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      相关资源
      最近更新 更多