本页内容

QGraphicsItemAnimation Class

QGraphicsItemAnimation 类为QGraphicsItem 提供了简单的动画支持。更多内容...

头文件: #include <QGraphicsItemAnimation>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承自: QObject
状态: 已弃用

该类已弃用。我们强烈建议不要在新代码中使用它。

公共函数

QGraphicsItemAnimation(QObject *parent = nullptr)
virtual ~QGraphicsItemAnimation()
void clear()
qreal horizontalScaleAt(qreal step) const
qreal horizontalShearAt(qreal step) const
QGraphicsItem *item() const
QPointF posAt(qreal step) const
QList<std::pair<qreal, QPointF>> posList() const
qreal rotationAt(qreal step) const
QList<std::pair<qreal, qreal>> rotationList() const
QList<std::pair<qreal, QPointF>> scaleList() const
void setItem(QGraphicsItem *item)
void setPosAt(qreal step, const QPointF &point)
void setRotationAt(qreal step, qreal angle)
void setScaleAt(qreal step, qreal sx, qreal sy)
void setShearAt(qreal step, qreal sh, qreal sv)
void setTimeLine(QTimeLine *timeLine)
void setTranslationAt(qreal step, qreal dx, qreal dy)
QList<std::pair<qreal, QPointF>> shearList() const
QTimeLine *timeLine() const
QTransform transformAt(qreal step) const
QList<std::pair<qreal, QPointF>> translationList() const
qreal verticalScaleAt(qreal step) const
qreal verticalShearAt(qreal step) const
qreal xTranslationAt(qreal step) const
qreal yTranslationAt(qreal step) const

公共槽

void setStep(qreal step)

受保护函数

virtual void afterAnimationStep(qreal step)
virtual void beforeAnimationStep(qreal step)

详细说明

QGraphicsItemAnimation 类用于对QGraphicsItem 进行动画处理。您可以按指定的步长安排对项的变换矩阵进行更改。QGraphicsItemAnimation 类有一个当前步长值。当该值发生变化时,将执行在该步长处安排的变换。动画的当前步长通过setStep() 函数设置。

QGraphicsItemAnimation 会对最近相邻的已安排变化进行简单的线性插值,以计算变换矩阵。例如,如果您将项目的位置设置为 0.0 和 1.0,动画将显示该项目在这两个位置之间沿直线移动。缩放和旋转也是如此。

通常将该类与QTimeLine 配合使用。此时,时间轴的valueChanged()信号需连接至setStep() 槽。例如,您可以通过调用setRotationAt() 并传入不同的步长值,来设置项目的旋转动画。动画的时间轴通过setTimeLine()函数进行设置。

以下是一个带时间轴的动画示例:

QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20);

QTimeLine *timer = new QTimeLine(5000);
timer->setFrameRange(0, 100);

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(ball);
animation->setTimeLine(timer);

for (int i = 0; i < 200; ++i)
    animation->setPosAt(i / 200.0, QPointF(i, i));

QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 250, 250);
scene->addItem(ball);

QGraphicsView *view = new QGraphicsView(scene);
view->show();

timer->start();

请注意,步长应在 0.0 到 1.0 之间。可能需要使用setUpdateInterval()。默认更新间隔为 40 毫秒。已设置的定时变换无法删除,因此不建议在同一步长内安排多个同类变换(例如旋转)。

另请参阅 ` QTimeLine ` 和 `Graphics View Framework`。

成员函数文档

QGraphicsItemAnimation::QGraphicsItemAnimation(QObject *parent = nullptr)

根据给定的parent 创建一个动画对象。

[virtual noexcept] QGraphicsItemAnimation::~QGraphicsItemAnimation()

销毁该动画对象。

[virtual protected] void QGraphicsItemAnimation::afterAnimationStep(qreal step)

该方法旨在供子类重写,以便在执行完一个新步骤后运行额外代码。提供了动画step ,供在操作取决于该值时使用。

[virtual protected] void QGraphicsItemAnimation::beforeAnimationStep(qreal step)

该方法旨在供需要在执行新步骤之前运行额外代码的子类进行重写。提供了step 动画,供在操作依赖于该值的情况下使用。

void QGraphicsItemAnimation::clear()

清除动画所用的预设变换,但保留项目和时间轴。

qreal QGraphicsItemAnimation::horizontalScaleAt(qreal step) const

返回指定step 值处该项目的水平缩放比例。

另请参阅 setScaleAt()。

qreal QGraphicsItemAnimation::horizontalShearAt(qreal step) const

返回指定step 值处该项的水平剪切量。

另请参阅 setShearAt()。

QGraphicsItem *QGraphicsItemAnimation::item() const

返回动画对象所作用的项。

另请参阅 setItem()。

QPointF QGraphicsItemAnimation::posAt(qreal step) const

返回在给定step 值处该项的位置。

另请参阅 setPosAt()。

QList<std::pair<qreal, QPointF>> QGraphicsItemAnimation::posList() const

返回所有显式插入的位置。

另请参阅 posAt() 和setPosAt()。

qreal QGraphicsItemAnimation::rotationAt(qreal step) const

返回项目围绕指定step 值旋转的角度。

另请参阅 setRotationAt()。

QList<std::pair<qreal, qreal>> QGraphicsItemAnimation::rotationList() const

返回所有显式插入的旋转。

另请参阅 rotationAt() 和setRotationAt()。

QList<std::pair<qreal, QPointF>> QGraphicsItemAnimation::scaleList() const

返回所有显式插入的刻度。

另请参阅 verticalScaleAt()、horizontalScaleAt(),以及setScaleAt()。

void QGraphicsItemAnimation::setItem(QGraphicsItem *item)

将指定的item 设置为动画中使用的对象。

另请参阅 item()。

void QGraphicsItemAnimation::setPosAt(qreal step, const QPointF &point)

将位于指定step 位置的项的位置设置为指定的point 。

另请参阅 posAt()。

void QGraphicsItemAnimation::setRotationAt(qreal step, qreal angle)

将位于指定step 值处的项的旋转角度设置为指定的angle 值。

另请参阅 rotationAt()。

void QGraphicsItemAnimation::setScaleAt(qreal step, qreal sx, qreal sy)

使用由sx 和sy 指定的水平和垂直缩放因子,将该项的缩放比例设置为给定的step 值。

另请参阅 verticalScaleAt() 和horizontalScaleAt()。

void QGraphicsItemAnimation::setShearAt(qreal step, qreal sh, qreal sv)

使用由sh 和sv 指定的水平和垂直剪切因子,将该项的剪切值设置为给定的step 值。

另请参阅 verticalShearAt() 和horizontalShearAt()。

[slot] void QGraphicsItemAnimation::setStep(qreal step)

设置动画的当前step 值,从而触发在此步骤中排定的变换操作。

void QGraphicsItemAnimation::setTimeLine(QTimeLine *timeLine)

将用于控制动画速率的时间轴对象设置为指定的timeLine 。

另请参阅 timeLine()。

void QGraphicsItemAnimation::setTranslationAt(qreal step, qreal dx, qreal dy)

使用dx 和dy 指定的水平和垂直坐标,将项目平移至给定的step 值处。

另请参阅 xTranslationAt() 和yTranslationAt()。

QList<std::pair<qreal, QPointF>> QGraphicsItemAnimation::shearList() const

返回所有显式插入的剪切变换。

另请参阅 verticalShearAt()、horizontalShearAt() 和setShearAt()。

QTimeLine *QGraphicsItemAnimation::timeLine() const

返回用于控制动画播放速率的时间轴对象。

另请参阅 setTimeLine()。

QTransform QGraphicsItemAnimation::transformAt(qreal step) const

返回用于指定step 值处该项的变换。

QList<std::pair<qreal, QPointF>> QGraphicsItemAnimation::translationList() const

返回所有显式插入的翻译。

另请参阅 xTranslationAt()、yTranslationAt() 和setTranslationAt()。

qreal QGraphicsItemAnimation::verticalScaleAt(qreal step) const

返回指定step 值处该项的垂直比例。

另请参阅 setScaleAt()。

qreal QGraphicsItemAnimation::verticalShearAt(qreal step) const

返回指定step 值处该项的垂直剪切量。

另请参阅 setShearAt()。

qreal QGraphicsItemAnimation::xTranslationAt(qreal step) const

返回指定step 值处项目的水平平移量。

另请参阅 setTranslationAt()。

qreal QGraphicsItemAnimation::yTranslationAt(qreal step) const

返回指定step 值处项的垂直平移量。

另请参阅 setTranslationAt()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.