在Qt中设置QGraphicsObject/QGraphicsItem的动画

animating a QGraphicsObject/QGraphicsItem in Qt

本文关键字:QGraphicsItem 动画 QGraphicsObject Qt 设置      更新时间:2023-10-16

情况:上层dialog生成具有正方形的光栅。玩家坐在(0,0(处,当用户点击正方形时,路径查找算法pathFind会生成玩家达到该目标所需方向的QString。然后将CCD_ 4转换为CCD_。

dialogmysquare类调用一个名为movePlayer的函数,该函数应将播放器移动到正确的方向。

这个动作不应该是瞬间发生的,而是以一定的速度发生的,这就是为什么我试图为每个动作设置动画,所以它需要一定的时间

然而,我的问题是玩家广场根本没有移动。有时,当我单击网格外的某个位置时,它会捕捉到结束位置。奇怪的

MySquare类(玩家(

 MySquare::MySquare(int x,int y, int w, int h){
        curX = x;
        curY = y;
    initX = x;
    initY = y;
    width = w;
    height = h;
    posAnimation.setPropertyName("getGridPos");
    posAnimation.setTargetObject(this);
}

bool MySquare::movePlayer(int direction){
    switch (direction){
    case 0: //move right
        curX+=width;
        break;
    case 1: //move up
        curY+=height;
        break;
    case 2: //move left
        curX+=-width;
        break;
    case 3: //move down
        curY+=-height;
        break;
    }
    //setPos(curX,curY);
    QPoint p;
    p.setX(curX);
    p.setY(curY);
    setGridPos(p);
    update();
    return true;
}
void MySquare::setGridPos(QPoint myPos) {
    posAnimation.setStartValue(pos());
    posAnimation.setDuration(2000); // 2 seconds
    posAnimation.setEndValue(myPos);
    posAnimation.start();
}

QPoint MySquare::getGridPos() const {
    return pos();
}

MySquare标题(玩家(

 class MySquare : public QGraphicsObject
    {
        Q_OBJECT
        Q_PROPERTY(QPoint getGridPos READ getGridPos WRITE setGridPos) // define meta-property "pos"s
    public:
    QPropertyAnimation posAnimation;
    MySquare(int x,int y,int h, int w);
    QRectF boundingRect() const; //outer most edges of the object
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool movePlayer(int direction);
public slots:
    QPoint getGridPos() const; // getter
    void setGridPos(QPoint p); // setter
signals:
    void targetChanged(int x, int y);
private:
    int curX,curY,height,width,initX,initY;
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};

编辑:

  • 我在setGridPos中添加了一些qDebug,它们都会立即打印出来;这告诉我脚本没有阻塞动画,这可能是问题的一部分

第2版:

  • 当我只调用movePlayer函数一次时,它似乎起作用

edit3:我有一种感觉,使用动画组可能会解决这个问题。

使用QSequentialAnimationGroup

从我第二次调用movePlayer开始,动画就停止工作了。我不得不重写dialog的一些部分:现在不再逐个给出方向,而是通过向量传递。我希望我没有使用"新"添加任何内存泄漏,但我不认为有任何方法可以绕过它。

 void MySquare::movePlayer(std::vector <int> directions){
        QSequentialAnimationGroup *group = new QSequentialAnimationGroup;
        //determine direction
        //& add each element to the group
        QPoint startPosition;
        startPosition.setX(pos().x());
        startPosition.setY(pos().y());
        for (int i=0; i<directions.size(); i++){
            int direction = directions[i];
            switch (direction){
            case 0: //move right
                curX+=width;
                break;
            case 1: //move up
                curY+=height;
                break;
            case 2: //move left
                curX+=-width;
                break;
            case 3: //move down
                curY+=-height;
                break;
            }
            QPoint endPosition;
            endPosition.setX(curX);
            endPosition.setY(curY);
            QPropertyAnimation *animation = new QPropertyAnimation(this,"getGridPos");
            animation->setStartValue(startPosition);
            animation->setDuration(1000); // 1 seconds
            animation->setEndValue(endPosition);
            group->addAnimation(animation); ////add animatons to a QSequentialAnimationGroup
            //delete animation;
            //update startPosition for next loop
            startPosition.setX(endPosition.x());
            startPosition.setY(endPosition.y());
        }
        qDebug() << "start animation group" << endl;
        group->start();
        update();
        //delete group;
    }