Reputation: 1539
I have created a object call Player
which inherits from QGraphicsObject
.
What I am trying to do is to change the image of the player and the colour of the bounding shape when I click on it with the mouse. The thing is i don't know what values to send to player->paint()
to update the image.
I override the two pure virtual functions as follows
In player.h :
class Player : public QGraphicsObject
{
public:
Player();
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
}
in player.cpp
Player::Player()
{
QPixmap m_playerPixmap(":/images/images/chevalier/test1.png");
}
QRectF Player::boundingRect() const
{
return QRectF(-15, 0, 128, 130);
}
QPainterPath Player::shape() const
{
QPainterPath path;
path.addEllipse(-15, 70, 100, 60);
return path;
}
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
QPen linepen;
linepen.setWidth(5);
linepen.setColor(Qt::red);
painter->setPen(linepen);
painter->drawPath(shape());
painter->drawPixmap(0, 0, m_playerPixmap);
}
void Player::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
this->paint();
}
Thanks a lot for your help.
Upvotes: 0
Views: 763
Reputation: 11513
You have to call update()
.
This will mark the item to be updated, and issue a paint event, which will then call paint
with the correct parameters
Upvotes: 2