【发布时间】:2015-12-23 14:31:23
【问题描述】:
我想在我的QGraphicsScene 中使用QPropertyAnimation 为QGraphicsPixmapItem 设置动画。
QPixmap pixmap(filePath);
QGraphicsItem *pixmapItem = graphicsScene->addPixmap(pixmap);
QPropertyAnimation animation(pixmapItem, "x"); // here lies the problem
animation.setDuration(30000);
// ...
animation.start();
我检查了文档,所以我已经知道 QGraphicsPixmapItem 及其父 QGraphicsItem 不是 QObject,并且带有 QGraphicsItem 的 QPropertyAnimation 构造函数不存在。根据各种消息来源,最好的方法是创建一个新类,例如AnimatedGraphicsItem 继承自 QObject 和 QGraphicsItem 或者我可以简单地继承自 QGraphicsObject。
class AnimatedGraphicsItem : public QObject, public QGraphicsItem // or public QGraphicsObject
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem) // not needed if I inherit from QGraphicsObject, right?
};
我需要在AnimatedGraphicsItem 类的头文件和源文件中准确包含哪些内容?一位消息人士告诉我也将其包含在头文件中,但我想,这已经被继承了?
public:
AnimatedGraphicsItem(QGraphicsItem *parent = 0);
private:
virtual QRectF boundingRect() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
我必须如何使用我的AnimatedGraphicsItem 类?
我必须将我的QGraphicsItem *pixmapItem 转换为AnimatedGraphicsItem 吗? (如果是,如何?)我是否必须创建 AnimatedGraphicsItem 的新实例?
我在互联网上没有找到一个很好的例子。
提前致谢!
【问题讨论】:
标签: c++ qt qgraphicsitem qpropertyanimation