如何使用动画改变QGraphicsPixmapitem的大小?

问题描述 投票:0回答:1

我有一个名为Pixmap的类派生自QGraphicsPixmapItem和源自QGraphicsScene的StartScreen类。我想使用动画(QPropertyAnimation类)在特定时间范围内调整显示图像的大小。设置位置或旋转等其他操作不是问题,但我找不到任何大小的属性(例如setSize()方法)。我怎么能以另一种方式做到这一点?谢谢你提前。

StartScreen::StartScreen(int windowWidth, int windowHeight)
{
    setSceneRect(0, 0, windowWidth, windowHeight);
    setBackgroundBrush(QBrush(QImage(":/images/background.png")));

    Pixmap * logo = new Pixmap(":/images/logo.png");
    addItem(logo);
    logo->setPos((windowWidth - logo->pixmap().width()) / 2, (windowWidth - logo->pixmap().width()) / 2 - 75);

    //QPropertyAnimation * animation = new QPropertyAnimation(logo, "");
}
c++ qt qpropertyanimation
1个回答
0
投票

QPropertyAnimation适用于Qt属性,但只有从QObject继承的对象具有Qt属性,因此如果您想使用动画,您可以使用QGraphicsObject并创建自己的项目,或创建一个继承您想要的项目和QObject的类。

class Pixmap: public QObject, public QGraphicsPixmapItem{

    Q_OBJECT
    Q_PROPERTY(qreal scale READ scale WRITE setScale)
    Q_PROPERTY(qreal rotation READ rotation WRITE setRotation)
    Q_PROPERTY(QPointF pos READ pos WRITE setPos)
public:
    using QGraphicsPixmapItem::QGraphicsPixmapItem;
};

在前面的例子中,利用QGraphicsItem及其派生类具有pos()setPos()scale()setScale()rotation()setRotation()等方法的事实,因此只能在Q_PROPERTY中使用它们。

在下一部分中,我展示了一个例子:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;

    QGraphicsScene scene(0, 0, 640, 480);
    w.setScene(&scene);
    w.show();

    Pixmap* logo = new Pixmap(QPixmap(":/image.jpg"));
    scene.addItem(logo);

    QSequentialAnimationGroup group;

    QPropertyAnimation animation_scale(logo, "scale");
    animation_scale.setDuration(1000);
    animation_scale.setStartValue(2.0);
    animation_scale.setEndValue(0.1);

    QPropertyAnimation animation_pos(logo, "pos");
    animation_pos.setDuration(1000);
    animation_pos.setStartValue(QPointF(0, 0));
    animation_pos.setEndValue(QPointF(100, 100));

    /**
    * it must indicate the center of rotation,
    * in this case it will be the center of the item
    */
    logo->setTransformOriginPoint(logo->boundingRect().center());
    QPropertyAnimation animation_rotate(logo, "rotation");
    animation_rotate.setDuration(1000);
    animation_rotate.setStartValue(0);
    animation_rotate.setEndValue(360);

    group.addAnimation(&animation_scale);
    group.addAnimation(&animation_pos);
    group.addAnimation(&animation_rotate);

    group.start();

    return a.exec();
}

#include "main.moc"

在下面的link中有一个例子

或者您可以使用QVariantAnimation而不是QPropertyAnimation:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;

    QGraphicsScene scene(0, 0, 640, 480);
    w.setScene(&scene);
    w.show();

    QGraphicsPixmapItem* logo = new QGraphicsPixmapItem(QPixmap(":/image.jpg"));
    scene.addItem(logo);

    QSequentialAnimationGroup group;

    QVariantAnimation animation_scale;
    animation_scale.setDuration(1000);
    animation_scale.setStartValue(2.0);
    animation_scale.setEndValue(0.5);

    QObject::connect(&animation_scale, &QVariantAnimation::valueChanged, [logo](const QVariant &value){
        logo->setScale(value.toReal());
    });

    animation_scale.start();

    QVariantAnimation animation_pos;
    animation_pos.setDuration(1000);
    animation_pos.setStartValue(QPointF(0, 0));
    animation_pos.setEndValue(QPointF(100, 100));

    QObject::connect(&animation_pos, &QVariantAnimation::valueChanged, [logo](const QVariant &value){
        logo->setPos(value.toPointF());
    });

    /**
    * it must indicate the center of rotation,
    * in this case it will be the center of the item
    */

    logo->setTransformOriginPoint(logo->boundingRect().center());
    QVariantAnimation animation_rotate;
    animation_rotate.setDuration(1000);
    animation_rotate.setStartValue(0);
    animation_rotate.setEndValue(360);

    QObject::connect(&animation_rotate, &QVariantAnimation::valueChanged, [logo](const QVariant &value){
        logo->setRotation(value.toReal());
    });

    group.addAnimation(&animation_scale);
    group.addAnimation(&animation_pos);
    group.addAnimation(&animation_rotate);

    group.start();

    return a.exec();
}

在下面的link中有一个例子

© www.soinside.com 2019 - 2024. All rights reserved.