【问题标题】:Easiest way to animate QGraphicsPixmapItem?动画 QGraphicsPixmapItem 的最简单方法?
【发布时间】:2015-12-23 14:31:23
【问题描述】:

我想在我的QGraphicsScene 中使用QPropertyAnimationQGraphicsPixmapItem 设置动画。

QPixmap pixmap(filePath);
QGraphicsItem *pixmapItem = graphicsScene->addPixmap(pixmap);

QPropertyAnimation animation(pixmapItem, "x"); // here lies the problem
animation.setDuration(30000);
// ...
animation.start();

我检查了文档,所以我已经知道 QGraphicsPixmapItem 及其父 QGraphicsItem 不是 QObject,并且带有 QGraphicsItemQPropertyAnimation 构造函数不存在。根据各种消息来源,最好的方法是创建一个新类,例如AnimatedGraphicsItem 继承自 QObjectQGraphicsItem 或者我可以简单地继承自 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


    【解决方案1】:

    好的,知道了。

    我必须实例化我的AnimatedGraphicsItem 对象,然后将setPixmapsetFlag、...和addItem 实例化到我的场景中(使用new 很重要)。

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多