Results 1 to 5 of 5

Thread: animating a QGraphicsObject

  1. #1
    Join Date
    Jan 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default animating a QGraphicsObject

    Hi; this is my first question on this forum

    **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`.

    `dialog`calls a function from the `mysquare`class 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)*

    Qt Code:
    1. MySquare::MySquare(int x,int y, int w, int h){
    2. curX = x;
    3. curY = y;
    4.  
    5. initX = x;
    6. initY = y;
    7. width = w;
    8. height = h;
    9. posAnimation.setPropertyName("getGridPos");
    10. posAnimation.setTargetObject(this);
    11. }
    12.  
    13.  
    14. bool MySquare::movePlayer(int direction){
    15. switch (direction){
    16. case 0: //move right
    17. curX+=width;
    18. break;
    19. case 1: //move up
    20. curY+=height;
    21. break;
    22. case 2: //move left
    23. curX+=-width;
    24. break;
    25. case 3: //move down
    26. curY+=-height;
    27. break;
    28. }
    29. //setPos(curX,curY);
    30. QPoint p;
    31. p.setX(curX);
    32. p.setY(curY);
    33.  
    34. setGridPos(p);
    35. return true;
    36. }
    37.  
    38. void MySquare::setGridPos(QPoint myPos) {
    39. posAnimation.setStartValue(pos());
    40. posAnimation.setDuration(2000); // 2 seconds
    41. posAnimation.setEndValue(myPos);
    42. posAnimation.start();
    43. }
    44.  
    45.  
    46. QPoint MySquare::getGridPos() const {
    47. return pos();
    48. }
    To copy to clipboard, switch view to plain text mode 


    *MySquare header (the player)*

    Qt Code:
    1. class MySquare : public QGraphicsObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QPoint getGridPos READ getGridPos WRITE setGridPos) // define meta-property "pos"s
    5. public:
    6. QPropertyAnimation posAnimation;
    7. MySquare(int x,int y,int h, int w);
    8. QRectF boundingRect() const; //outer most edges of the object
    9. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    10. bool movePlayer(int direction);
    11. public slots:
    12. QPoint getGridPos() const; // getter
    13. void setGridPos(QPoint p); // setter
    14.  
    15. signals:
    16. void targetChanged(int x, int y);
    17.  
    18. private:
    19. int curX,curY,height,width,initX,initY;
    20. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    21. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    22. void keyPressEvent(QKeyEvent *event);
    23. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    24. };
    To copy to clipboard, switch view to plain text mode 

    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.

    * The keyboard keys are also implemented to be able to move the player using the setPos() functions which are part of QGraphicsItem. When I press a key after clicking on a square it jumps to the corrent square (plus the key i've pressed that is) so it is updating the position of the square but somehow not showing it in the gui.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: animating a QGraphicsObject

    It isn't clear to me how a QPoint would be animated in the first place, since you have two parameters (x and y). Have you tried an animation of just one of the two coordinates, or a parallel animation of both? Do you need to specify some trajectory before a QPoint can be animated, or is there a default, like delta x = delta y?

    I notice you have posted exactly the same question to stackoverflow. That is not considered good etiquette. Post in one place, if you don't get an answer in a reasonable amount of time (a day), then post somewhere else, but never simultaneously in two or more places.
    Last edited by d_stranz; 6th January 2013 at 19:54.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: animating a QGraphicsObject

    Please post some compilable code demonstrating the problem. What you posted looks ok and in general should work, provided you call setGridPos() correctly.

    @d_stranz: animating a point is not a problem, Qt knows how to do that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    d_stranz (7th January 2013)

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: animating a QGraphicsObject

    animating a point is not a problem, Qt knows how to do that
    So I guess the default easing curve for moving from point A to point B is linear? - Divide delta x and delta x by the time and move each incrementally by the appropriate amount at each step?

    I don't see where QAbstractAnimation or its derived classes offer any control over the step size (time increment), only the duration of the animation. QEasingCurve allows control of the rate (i.e acceleration) of increment, but not of the increment itself. Is there a way to specify, f.e. that you want an animation that lasts for 5 seconds, and requires 10 steps? (I mean, maybe for some silly reason, you might want a jerky animation. Maybe you are trying to simulate an old silent movie).

    (Looking at QEasingCurve, I suppose you could override the QEasingFunction::customType method to achieve a "jerky" motion by returning the same value for some range of progress values).
    Last edited by d_stranz; 7th January 2013 at 06:34.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: animating a QGraphicsObject

    Quote Originally Posted by d_stranz View Post
    So I guess the default easing curve for moving from point A to point B is linear?
    The default easing curve is always linear. What's important is that QVariantAnimation knows how to interpolate a QPoint.

    I don't see where QAbstractAnimation or its derived classes offer any control over the step size (time increment),
    They don't.

    Is there a way to specify, f.e. that you want an animation that lasts for 5 seconds, and requires 10 steps?
    No.
    (I mean, maybe for some silly reason, you might want a jerky animation. Maybe you are trying to simulate an old silent movie).
    In that case use a simple QTimer. Alternatively you might implement a custom easing curve but I don't think it is worth it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    d_stranz (7th January 2013)

Similar Threads

  1. QGraphicsObject Performance Issue
    By alizadeh91 in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2012, 18:41
  2. QGraphicsObject color change animation
    By mvbhavsar in forum Newbie
    Replies: 8
    Last Post: 30th August 2011, 14:34
  3. How to make an QGraphicsObject animation?
    By luisvaldes88 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2011, 19:01
  4. protected QGraphicsObject
    By stefan in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2011, 18:04
  5. Replies: 1
    Last Post: 24th October 2010, 03:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.