Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9083043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:41:29+00:00 2026-06-16T20:41:29+00:00

Situation : An upper class dialog generates a raster with squares. A player sits

  • 0

Situation:
An upper class dialog generates a raster with squares. A player sits at (0,0) and when the user clicks on a square a pathfinding algoritm pathFind generates a QString of directions the player has to take to reach this goal. The QString is then converted to an array of int.

dialogcalls a function from the mysquareclass named movePlayer which should move the player in the correct direction.

This movement should not happen instantaneous but at a certain rate which is why I’m trying to animate each movement so it takes a certain amount of time.

My problem however is that the player square is not moving at all. Sometimes when I click somewhere outside of the grid it snaps to the ending position. Weird.

MySquare class (the player)

 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 header (the player)

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

edit:

  • I’ve added some qDebug inside the setGridPos which are all immediately printed; which is telling me that the script is not blocking on the animation which could be part of the problem.

edit2:

  • It seems it works when I only call the movePlayer function once;

edit3: I have a feeling it might be resolved using an animation group.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:41:31+00:00Added an answer on June 16, 2026 at 8:41 pm

    Use a QSequentialAnimationGroup.

    From the second time I called movePlayer the animation stops working. I had to rewrite some parts of the dialog aswel: now the directions are no longer given one by one but are passed trough a vector. I hope I didn’t add any memory leaks using the ‘new’ but I don’t think there is any way around it.

     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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation: a user clicks on a link, the server gets the request and starts
Situation is simple: I post a plain HTML form with a textarea. Then, in
Situation: I'm working with Adobe Livecycle ES2 and Flex In our project we show
Situation: 1. Linux TCP server 2 Windows C# client application The server receives messages
Situation: I have an index page which loads pages into it with ajax as
Situation: I often run into this problematic and never know how to solve it.
Situation is simple. I need to wrap text around two inline objects (buttons with
Situation: My tooltips show up on my page. Opening my fancybox works. Doing the
Situation: I have some persons with certain skills and they can/might belong to more
Situation is you have to develop an application against an Active Directory Tree. Want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.