'Setting offset between two rotating QGraphicsRectItems
I have a red rectangle (QGraphicsRectItem), which position and rotation depends on pressed keys (up, down, left, right, turn right, turn left). I want another - green rectangle to follow red's position and rotation, but with a certain offset, in this manner:
I'd like to know how to achieve it without making the red parent of the green. My code (withotu the desired offset) is:
Game::Game()
{
player->setRect(0,0,100,100);
player->setBrush(Qt::red);
player->setPos(400,300);
this->addItem(player);
player->setFocus();
rectItem->setRect(0,0,100,100);
rectItem->setBrush(Qt::green);
rectItem->setPos(400,330);
this->addItem(rectItem);
player->setZValue(10);
rectItem->setZValue(1);
player->setTransformOriginPoint(player->boundingRect().width()/2 ,
player->boundingRect().height()/2 );
rectItem->setTransformOriginPoint(player->transformOriginPoint());
}
void Game::advance()
{
rectItem->setPos(player->pos().x(), player->pos().y());
rectItem->setRotation(player->rotation());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

