Qt 图形项通过QGraphicsSiteManimation跳转到路径的末尾而不移动
我有一个圆,我想在路径上平稳移动。path类类似于从QPainterPath派生的水平U。当我启动计时器(QTimeLine对象)时,圆只是从路径的起点跳到终点(上U分叉的起点到下分叉的终点),没有平滑的动画。不幸的是,QTimeLine::setLoopCount(int n)也不起作用 你知道原因吗Qt 图形项通过QGraphicsSiteManimation跳转到路径的末尾而不移动,qt,animation,Qt,Animation,我有一个圆,我想在路径上平稳移动。path类类似于从QPainterPath派生的水平U。当我启动计时器(QTimeLine对象)时,圆只是从路径的起点跳到终点(上U分叉的起点到下分叉的终点),没有平滑的动画。不幸的是,QTimeLine::setLoopCount(int n)也不起作用 你知道原因吗 // UPath(int forkLen, int forksDistance, QPointF startPoint) UPath* uPath = new UPath(500, 60, QP
// UPath(int forkLen, int forksDistance, QPointF startPoint)
UPath* uPath = new UPath(500, 60, QPointF(10, 10));
QList<QPointF> points = uPath->pathPoints(0.006); // returns the points of the path
// implemented by QPainterPath::pointAtPercent()
QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 10, 10);
QTimeLine *timer = new QTimeLine(5000);
timer->setFrameRange(0, 100);
timer->setLoopCount(2); // doesn't work
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(ball);
animation->setTimeLine(timer);
for (int i = 0; i < points.count(); ++i)
animation->setPosAt(i/points.count(), points.at(i));
QGraphicsScene *scene = new QGraphicsScene();
scene->addItem(ball);
QGraphicsView *view = new QGraphicsView(scene);
view->setRenderHint(QPainter::Antialiasing);
view->show();
timer->start();
//UPath(int-forkLen、int-forksDistance、QPointF-startPoint)
UPath*UPath=新的UPath(500,60,QPointF(10,10));
QList points=uPath->pathPoints(0.006);//返回路径的点
//由QPainterPath::pointAtPercent()实现
QGraphicsItem*ball=新的QGraphicsEllipseItem(0,0,10,10);
QTimeLine*定时器=新的QTimeLine(5000);
定时器->设置帧范围(0,100);
计时器->设置循环计数(2);//不起作用
QGraphicsSiteManimation*动画=新的QGraphicsSiteManimation;
动画->设置项(球);
动画->设置时间线(计时器);
对于(int i=0;isetPosAt(i/points.count(),points.at(i));
qgraphicscene*场景=新的qgraphicscene();
场景->附加项(球);
QGraphicsView*视图=新的QGraphicsView(场景);
视图->SetRenderInt(QPaint::抗锯齿);
查看->显示();
定时器->启动();
已弃用QGraphicsAnimation
类。您需要的是QPainterPath
和动画系统之间的适配器。请参阅下面的完整示例
在动画中使用画师路径需要一些额外的平滑(重采样),因为路径上的速度会发生变化,看起来也不太好。运行下面的代码时,您可能会注意到它。绘制路径是用于绘制的,而不是用于设置动画
这种错误行为的程度将取决于您所使用的路径的类型,因此对于您所拥有的特定用例,它可能最终会正常工作
#include <QApplication>
#include <QAbstractAnimation>
#include <QPainterPath>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsEllipseItem>
#include <QDebug>
class PathAnimation : public QAbstractAnimation {
Q_OBJECT
Q_PROPERTY(int duration READ duration WRITE setDuration)
QPainterPath m_path;
int m_duration;
QVector<QPointF> m_cache;
QGraphicsItem * m_target;
int m_hits, m_misses;
public:
PathAnimation(const QPainterPath & path, QObject * parent = 0) :
QAbstractAnimation(parent), m_path(path), m_duration(1000), m_cache(m_duration), m_target(0), m_hits(0), m_misses(0) {}
~PathAnimation() { qDebug() << m_hits << m_misses; }
int duration() const { return m_duration; }
void setDuration(int duration) {
if (duration == 0 || duration == m_duration) return;
m_duration = duration;
m_cache.clear();
m_cache.resize(m_duration);
}
void setTarget(QGraphicsItem * target) {
m_target = target;
}
void updateCurrentTime(int ms) {
QPointF point = m_cache.at(ms);
if (! point.isNull()) {
++ m_hits;
} else {
point = m_path.pointAtPercent(qreal(ms) / m_duration);
m_cache[ms] = point;
++ m_misses;
}
if (m_target) m_target->setPos(point);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsEllipseItem * item = new QGraphicsEllipseItem(-5, -5, 10, 10);
item->setPen(QPen(Qt::red, 2));
item->setBrush(Qt::lightGray);
QPainterPath path;
path.addEllipse(0, 0, 100, 100);
PathAnimation animation(path);
animation.setTarget(item);
QGraphicsScene scene;
scene.addItem(item);
QGraphicsView view(&scene);
view.setSceneRect(-50, -50, 200, 200);
animation.setLoopCount(-1);
animation.start();
view.show();
return a.exec();
}
#include "main.moc"
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类PathAnimation:公共QabStretchAnimation{
Q_对象
Q_属性(int duration READ duration WRITE setDuration)
QPainterPath m_path;
int m_持续时间;
qvectorm_缓存;
QGraphicsItem*m_目标;
int m_命中,m_未命中;
公众:
路径动画(常量QPainterPath&path,QObject*parent=0):
QAbstractAnimation(父)、m_路径(path)、m_持续时间(1000)、m_缓存(m_持续时间)、m_目标(0)、m_命中(0)、m_未命中(0){}
~PathAnimation(){qDebug()setPen(QPen(Qt::red,2));
项目->退刀(Qt::浅灰色);
QPainterPath路径;
路径加法(0,0,100,100);
路径动画(路径);
动画。设置目标(项目);
QsCENE场景;
场景.附加项(项目);
QGraphicsView视图(和场景);
视图.setscen直立(-50,-50200200);
动画.setLoopCount(-1);
animation.start();
view.show();
返回a.exec();
}
#包括“main.moc”
除非我误解了这一点,否则您对uPath->pathPoints(0.006)的注释说明它是由QPainterPath::pointAtPercent实现的,它只返回一个点,但您需要检索所有点……或者您的uPath->pathPoints(0.006)是否实际返回多个点?如果是,0.006是什么?