首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QPropertyAnimation for QGraphicsPolygonItem不工作

QPropertyAnimation for QGraphicsPolygonItem不工作
EN

Stack Overflow用户
提问于 2014-08-14 12:03:18
回答 1查看 536关注 0票数 0

我有一个从QGraphicsPolygonItem派生出来的类。在里面有一个功能,负责动画。该函数看起来如下:

代码语言:javascript
复制
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()如下所示:

代码语言:javascript
复制
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多边形读取polygonNew写setPolygonNew) PolygonNew和setPolygonNew调用QGraphicsPolygonItem类的多边形()和setPolygon()。

因此,动画已启动,但无法工作,我不确定它是否应该对多边形项目起作用。在动画的开头,polygonNew被调用了三次,setPolygonNew根本没有被调用。有没有人知道我该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-15 07:56:45

QPolygonF不是QPropertyAimation支持的类型。你可以看到这里支持的类型

您必须提供自己的插值函数,以使它与QPolygonF一起工作。

下面是Qt文档提供的一个示例

代码语言:javascript
复制
QVariant myColorInterpolator(const QColor &start, const QColor &end, qreal progress)
{
    ...
    return QColor(...);
}
...
qRegisterAnimationInterpolator<QColor>(myColorInterpolator);

下面是如何使用QPolygonF实现这一任务

mainwindow.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25307615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档