0

I am designing a timer with Qt. With QGraphicsEllipseItem, I drew a circle and now I need to animate the QPen around this circle (change color) every second. I found QGraphicsPathItem, but I need some examples on how to move forward. Can anyone show me an example?

halfer
  • 19,824
  • 17
  • 99
  • 186
jxgn
  • 741
  • 2
  • 15
  • 36

2 Answers2

1

You have two problems:

  1. QGraphicsEllipseItem is not a QObject so QPropertyAnimation can't be used directly on this item
  2. QGraphicsItemAnimation doesn't cover property you want to animate.

What you can do?
IMO best approach is to provide some custom QObject on which you could do this animation. You can inherit QObject or use fake QGraphicsObject (which is a QObject).

class ShapeItemPenAnimator : public QGraphicsObject {
    Q_OBJECT
private:
    QAbstractGraphicsShapeItem *mParent;
    QPropertyAnimation *mAnimation;

public:
    QPROPERTY(QColor penColor
              READ penColor
              WRITE setPenColor)

    explicit ShapeItemPenAnimator(QAbstractGraphicsShapeItem * parent)
    : QGraphicsObject(parent)
    , mParent(parent) {
       setFlags(QGraphicsItem::ItemHasNoContents);
       mAnimation = new QPropertyAnimation(this, "penColor", this);
    }

    QColor penColor() const {
        return mParent->pen().color();
    }
public slots:
    void setPenColor(const QColor &color) {
         QPen pen(mParent->pen());
         pen.setColor(color);
         mParent->setPen(pen);
    }

public:
    void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0) {
    }

    QRectF boundingRect() const {
         return QRectF();
    }

    QPropertyAnimation *animation() const {
        return mAnimation;
    }
}

Now you just attach this object to your QGraphicsEllipseItem and set animation you need.

// yourEllipse
ShapeItemPenAnimator *animator = new ShapeItemPenAnimator(yourEllipse);
animator->animation()->setEndValue(....);
animator->animation()->setStartValue(....);
animator->animation()->setDuration(....);
animator->animation()->setEasingCurve(....);
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thanks for your time. I am trying to understand this whole graphics in Qt. So sorry if I ask any lame questions. For now I am just trying to run your example and see what happens. So I passed QGraphicsEllipseItem to the ShapeItemPenAnimator and got this error, "invalid new-expression of abstract class"... – jxgn May 19 '15 at 09:22
  • just implement missing methods. probably `boindingRect`. – Marek R May 19 '15 at 09:56
  • Hi again, with property animation, the animations are pre-defined and then we start it. Here, in this case, I don't understand how it works? I have passed the graphicsview object in setTargetObject(), is that correct? – jxgn May 20 '15 at 05:26
  • `ShapeItemPenAnimator` is just proxy object which can expose property since it is a `QObject`. It does't do anything else. Like I wrote you could inherit `QObject` with same result, but in such case memory management is harder (that is why I used `QGraphicsObject`). About property animation read its documentation. This `setTarget` I wrote by mistake it shouldn't be there (I've fixed that). – Marek R May 20 '15 at 07:18
0

There are several classes helping with animations of QGraphicsItem in Qt. I suggest looking into QGraphicsItemAnimation and QPropertyAnimation. You can use the second one to animate the color of an item. Here is an example of using QPropertyAnimation: How to make Qt widgets fade in or fade out?

Community
  • 1
  • 1
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92