【问题标题】:Calling Qpainter's method paint to refresh image and change color调用Qpainter的方法paint刷新图像并改变颜色
【发布时间】:2015-04-01 14:51:21
【问题描述】:

我创建了一个对象调用Player,它继承自QGraphicsObject

当我用鼠标单击它时,我要做的是更改播放器的图像和边界形状的颜色。问题是我不知道要向player->paint() 发送什么值来更新图像。

我重写两个纯虚函数如下

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

在 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();
}

非常感谢您的帮助。

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    您必须致电update()

    这将标记要更新的项目,并发出一个绘制事件,然后使用正确的参数调用paint

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 2016-03-20
      • 2013-03-26
      • 1970-01-01
      • 2013-05-26
      相关资源
      最近更新 更多