// main file: Creating scene, view and a circle item. Adding the circle to scene and calling the animation funstion.
ui->testView->setScene(scene); // testView is object of QGraphicsView
CircleItem *circle = new CircleItem ();
scene->addItem(circle);
circle->animate();
// CircleItem.h : Creating custom item which is basically a circle.
{
Q_OBJECT
Q_PROPERTY (qreal scale READ scale WRITE setScale)
private:
qreal _radius;
qreal _scale;
QPropertyAnimation *_anim;
public:
~CircleItem ()
{
}
void animate ();
};
// CircleItem.cpp : The scale of the circle is being animated.
{
_radius = 5.0;
_scale = 3.0;
}
QRectF CircleItem
::boundingRect() const {
double rect = _radius * _scale;
return QRectF (-rect,
-rect,
2 * rect,
2 * rect
);
}
{
QRectF rect
( -_radius,
-_radius,
2 * _radius,
2 * _radius
);
painter
->setRenderHint
(QPainter::Antialiasing);
painter
->setPen
(QPen (Qt
::cyan,
2, Qt
::SolidLine, Qt
::RoundCap));
painter->drawEllipse (rect);
}
// Repeatedly animating the scale.
void CircleItem::animate ()
{
_anim = new QPropertyAnimation (this, "scale");
_anim->setEasingCurve (QEasingCurve::Linear);
_anim->setDuration (1000);
_anim->setStartValue (0);
_anim->setEndValue (_scale);
_anim->setLoopCount (-1);
_anim->start(QAbstractAnimation::DeleteWhenStopped);
}
// main file: Creating scene, view and a circle item. Adding the circle to scene and calling the animation funstion.
scene = new QGraphicsScene (this);
ui->testView->setScene(scene); // testView is object of QGraphicsView
CircleItem *circle = new CircleItem ();
scene->addItem(circle);
circle->animate();
// CircleItem.h : Creating custom item which is basically a circle.
class CircleItem : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_PROPERTY (qreal scale READ scale WRITE setScale)
private:
qreal _radius;
qreal _scale;
QPropertyAnimation *_anim;
public:
CircleItem (QGraphicsItem *parent = 0);
~CircleItem ()
{
}
QRectF boundingRect () const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void animate ();
};
// CircleItem.cpp : The scale of the circle is being animated.
CircleItem::CircleItem (QGraphicsItem *parent)
{
_radius = 5.0;
_scale = 3.0;
}
QRectF CircleItem::boundingRect() const
{
double rect = _radius * _scale;
return QRectF (-rect, -rect, 2 * rect, 2 * rect);
}
void CircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect ( -_radius, -_radius, 2 * _radius, 2 * _radius);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen (QPen (Qt::cyan, 2, Qt::SolidLine, Qt::RoundCap));
painter->drawEllipse (rect);
}
// Repeatedly animating the scale.
void CircleItem::animate ()
{
_anim = new QPropertyAnimation (this, "scale");
_anim->setEasingCurve (QEasingCurve::Linear);
_anim->setDuration (1000);
_anim->setStartValue (0);
_anim->setEndValue (_scale);
_anim->setLoopCount (-1);
_anim->start(QAbstractAnimation::DeleteWhenStopped);
}
To copy to clipboard, switch view to plain text mode
Bookmarks