darksaga
6th September 2007, 13:21
hi there,
i've subclassed a QGraphicsItem to create a rotating & clickable icon for a QGraphicsScene,
rotation works fine, using QTimeLine & QGraphicsItemAnimation,
the problem that I'm facing now is that my item rotates around the coordinates 0/0,
how do I tell my item to rotate around its center: _with/2 / _height/2?
regards
ps: here's the code:
#ifndef ANIMATEICON_H
#define ANIMATEICON_H
#include <QObject>
#include <QGraphicsItem>
#include <QRectF>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QWidget>
#include <QTimeLine>
#include <QGraphicsItemAnimation>
class AnimateIcon : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
AnimateIcon(QGraphicsItem *parent = 0, int width = 48, int height = 48) : QGraphicsItem(parent){
_width = width;
_height = height;
_animation = new QGraphicsItemAnimation(this);
_timeLine = new QTimeLine(10000, this);
_animation->setItem(this);
_animation->setTimeLine(_timeLine);
for (int i = 0; i <= 360; i+=18)
_animation->setRotationAt(i/360.0, i);
_timeLine->setCurveShape(QTimeLine::LinearCurve);
_timeLine->setLoopCount(0);
_timeLine->start();
}
~AnimateIcon(void){}
QRectF boundingRect(void) const{
return QRectF(0, 0, _width, _height);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget *widget = 0){
painter->drawPixmap(0, 0, _width, _height,QPixmap(":/icons/action.png"));
}
private:
int _width;
int _height;
QGraphicsItemAnimation *_animation;
QTimeLine *_timeLine;
};
#endif
i've subclassed a QGraphicsItem to create a rotating & clickable icon for a QGraphicsScene,
rotation works fine, using QTimeLine & QGraphicsItemAnimation,
the problem that I'm facing now is that my item rotates around the coordinates 0/0,
how do I tell my item to rotate around its center: _with/2 / _height/2?
regards
ps: here's the code:
#ifndef ANIMATEICON_H
#define ANIMATEICON_H
#include <QObject>
#include <QGraphicsItem>
#include <QRectF>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QWidget>
#include <QTimeLine>
#include <QGraphicsItemAnimation>
class AnimateIcon : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
AnimateIcon(QGraphicsItem *parent = 0, int width = 48, int height = 48) : QGraphicsItem(parent){
_width = width;
_height = height;
_animation = new QGraphicsItemAnimation(this);
_timeLine = new QTimeLine(10000, this);
_animation->setItem(this);
_animation->setTimeLine(_timeLine);
for (int i = 0; i <= 360; i+=18)
_animation->setRotationAt(i/360.0, i);
_timeLine->setCurveShape(QTimeLine::LinearCurve);
_timeLine->setLoopCount(0);
_timeLine->start();
}
~AnimateIcon(void){}
QRectF boundingRect(void) const{
return QRectF(0, 0, _width, _height);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget *widget = 0){
painter->drawPixmap(0, 0, _width, _height,QPixmap(":/icons/action.png"));
}
private:
int _width;
int _height;
QGraphicsItemAnimation *_animation;
QTimeLine *_timeLine;
};
#endif