【问题标题】:QPropertyAnimation for QGraphicsPolygonItem doesn't workQGraphicsPolygonItem 的 QPropertyAnimation 不起作用
【发布时间】:2014-08-14 12:03:18
【问题描述】:

我有一个派生自 QGraphicsPolygonItem 的类。里面有一个负责动画的函数。函数如下:

void DrawBase::makeAnimation(){
        /* creating 2 states */
        QState* st1 = new QState();
        QState* st2 = new QState();

        st1->addTransition(this, SIGNAL(clicked()), st2);
        st2->addTransition(this, SIGNAL(clicked()), st1);

        /* adding states to state machine */
        _stateMachine.addState(st1);
        _stateMachine.addState(st2);
        _stateMachine.setInitialState(st1);

        QObject::connect(st1, SIGNAL(entered()), this, SLOT(animate1()));
        QObject::connect(st2, SIGNAL(entered()), this, SLOT(animate2()));

        /* starting machine */
        _stateMachine.start();
}

连接的插槽 animate1() 和 animate2() 看起来像:

void DrawBase::animate1()
{
    qDebug() << "Animation 1";
    animation = new QPropertyAnimation(this, "polygon");
    animation->setDuration(1000);

    animation->setStartValue(this->polygon());

    QTransform trans;
    trans=trans.scale(0.5,0.5);
    QPolygonF newPoly=trans.map(this->polygon());

    animation->setEndValue(newPoly);

    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

QPropertyAnimation 没有看到 Polygon 属性,所以我在标题中定义了该属性,如下所示:

Q_PROPERTY (QPolygonF 多边形 READ polygonNew WRITE setPolygonNew) PolygonNew 和 setPolygonNew 调用 QGraphicsPolygonItem 类的 polygon() 和 setPolygon()。

结果动画已启动但无法正常工作,我不确定它是否适用于多边形项目。在动画开始的时候,polygonNew 被调用了 3 次,setPolygonNew 根本没有被调用。有人对我如何使它起作用有想法吗?

【问题讨论】:

    标签: qt qt4 qt5 qgraphicsscene


    【解决方案1】:

    QPolygonF 不是 QPropertyAimation 支持的类型。你可以看到supported types here

    您必须提供自己的插值函数才能使其与QPolygonF 一起使用。

    这是Qt docs提供的示例:

    QVariant myColorInterpolator(const QColor &start, const QColor &end, qreal progress)
    {
        ...
        return QColor(...);
    }
    ...
    qRegisterAnimationInterpolator<QColor>(myColorInterpolator);
    

    这里是如何使用QPolygonF

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        Q_PROPERTY(QPolygonF polygon READ getPolygon WRITE setPolygon)
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void setPolygon(QPolygonF polygon);
        QPolygonF getPolygon() const;
    
    private:
        Ui::MainWindow *ui;
        QPolygonF poly;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPropertyAnimation>
    
    QVariant myPolygonInterpolator(const QPolygonF &start, const QPolygonF &end, qreal progress)
    {
        if(progress < 1.0)
            return start;
        else
            return end;
    }
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        qRegisterAnimationInterpolator<QPolygonF>(myPolygonInterpolator);
        poly << QPoint(10,0);
        QPropertyAnimation *animation = new QPropertyAnimation(this, "polygon");
        animation->setDuration(1000);
        QPolygonF start;
        start << QPoint(0, 0);
        animation->setStartValue(start);
        QPolygonF end;
        end << QPoint(100, 100);
        animation->setEndValue(end);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::setPolygon(QPolygonF polygon)
    {
        poly = polygon;
    }
    
    QPolygonF MainWindow::getPolygon() const
    {
        return poly;
    }
    

    【讨论】:

    • 您能否评论一下插值器的放置位置以及如何注册它?我试过了,但对我来说它不起作用。我也没有在互联网上找到任何好的例子。
    • @AlexanderTyapkov 您必须在构建动画之前注册它。例如在DrawBase 构造函数中。我编辑了我的答案以提供一个工作示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多