2014-05-06 25 views
0

我想通過鼠標的位置旋轉QGraphicsItem。所以在我的Dialog.cpp,我調用這個函數,每當我的鼠標POS變化:Qt旋轉/移動相對於mousepos的自定義項目

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

其中(1100,1300)是其中的玩家的位置。 我很確定這會做到這一點,但它不能正確旋轉。也許本地位置不正確?

另外,我該如何移動播放器以便旋轉不會影響它?任何輸入將不勝感激。提前致謝。

回答

0

花了我一些時間,但我終於明白了!

函數旋轉(INT,INT)的第一行應是:

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