简体   繁体   English

如何在Qt中固定点周围的线动画?

[英]How to animate line around fixed point in Qt?

I'm trying to animate a line around a fixed point in Qt. 我正在尝试为Qt中的固定点周围的线设置动画。 I assume I need to use the QPropertyAnimation class to do this but cannot figure out which property to use. 我假设我需要使用QPropertyAnimation类来执行此操作,但无法确定要使用哪个属性。

For clarity, here's what I'm trying to do. 为了清楚起见,这是我要尝试做的事情。

|        (5, 10)
|       /
|      /
|     /
|    /          (10, 5)   
|   /          .
|  /
| /           
|/
|--------------------------
 ^
 |---(0,0)

Given (x1, y1) = (0, 0) & (x2, y2) = (5, 10), this would be the first frame of the animation. 给定(x1,y1)=(0,0)&(x2,y2)=(5,10),这将是动画的第一帧。 I'd like to then do a smooth animation from (x1, y1), (x2, y2), (with (x1, y1) being one end of the line and (x2, y2) being the other end) to (x1, y1), (x3, y3), with (x3, y3) = (10, 5). 然后,我想做一个平滑的动画,从(x1,y1),(x2,y2),(其中(x1,y1)是直线的一端,(x2,y2)是另一端)到(x1 ,y1),(x3,y3),(x3,y3)=(10,5)。 Similar to how a clock hand is animated. 类似于钟针的动画制作。 And before someone posts the analog clock example it uses a rotating pixmap which is not what I need. 在有人发布模拟时钟示例之前,它使用了旋转pixmap ,这不是我所需要的。

I haven't found a whole lot of information on Qt animations, just a lot of basic GUI tutorials. 我没有找到有关Qt动画的大量信息,只是找到了许多基本的GUI教程。

I have tried doing the following 我尝试了以下操作

QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry")

and the problem with this method is that in this technique the widget is moved between 2 points based on (0, 0) of the widget using the ->setStartValue(startX, startY, ...) and does not allow me to keep one of my lines at a fixed point. 这种方法的问题在于,在这种技术中,使用-> setStartValue(startX,startY,...),基于(0,0),在小部件的(0,0)之间将小部件移动2个点,并且不允许我保留一个固定点上的线数

and

QPropertyAnimation *anim = new QPropertyAnimation(widget, "rotation")

The problem with this method being similar to geometry in that it rotates said widget along a (0, 0) point. 该方法的问题类似于几何,因为它使所述控件沿(0,0)点旋转。

Can someone tell me how to achieve the desired effect? 有人可以告诉我如何达到预期的效果吗?

Thanks. 谢谢。

QGraphicsXXXItem do not support q-properties so they can not be used with QPropertyAnimation directly. QGraphicsXXXItem不支持q属性,因此它们不能直接与QPropertyAnimation一起使用。 So the solution is to create a class that inherits QObject and QGraphicsLineItem , plus we must add a q-property that handles the p2 position of the QLineF associated with the line as shown below: 因此,解决方案是创建一个继承QObjectQGraphicsLineItem的类,此外,我们必须添加一个q属性,该属性处理与行关联的QLineFp2位置,如下所示:

lineitem.h lineitem.h

#ifndef LINEITEM_H
#define LINEITEM_H

#include <QGraphicsLineItem>
#include <QObject>

class LineItem: public QObject, public QGraphicsLineItem {
    Q_OBJECT
    Q_PROPERTY(QPointF p1 READ p1 WRITE setP1)
    Q_PROPERTY(QPointF p2 READ p2 WRITE setP2)

public:
    using QGraphicsLineItem::QGraphicsLineItem;

    QPointF p1() const {
        return  line().p1();
    }

    void setP1(const QPointF & p){
        QLineF l = line();
        l.setP1(p);
        setLine(l);
    }

    QPointF p2() const {
        return  line().p2();
    }

    void setP2(const QPointF & p){
        QLineF l = line();
        l.setP2(p);
        setLine(l);
    }
};

#endif // LINEITEM_H

main.cpp main.cpp

#include "lineitem.h"

#include <QApplication>
#include <QGraphicsView>
#include <QPropertyAnimation>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene(-100, -100, 200, 200);
    QGraphicsView view(&scene);

    QGraphicsLineItem *item = scene.addLine(QLine(0, 100, 100, 0));
    item->setPen(QPen(Qt::red, 5));

    LineItem *lineItem = new LineItem(QLineF(QPointF(0, 0), QPointF(0, 100)));
    scene.addItem(lineItem);
    lineItem->setPen(QPen(Qt::green, 2));

    QPropertyAnimation *anim = new QPropertyAnimation(lineItem, "p2");
    anim->setStartValue(QPointF(0, 100));
    anim->setEndValue(QPointF(100, 0));
    anim->setDuration(2000);
    anim->start();

    view.resize(640, 480);
    view.show();
    return a.exec();
}

Another way is to use QVariantAnimation: 另一种方法是使用QVariantAnimation:

#include <QApplication>
#include <QGraphicsView>
#include <QPropertyAnimation>
#include <QGraphicsLineItem>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene(-100, -100, 200, 200);
    QGraphicsView view(&scene);

    scene.addLine(QLine(0, 100, 100, 0), QPen(Qt::green));

    QGraphicsLineItem *item = scene.addLine(QLine(0, 0, 0, 100));
    item->setPen(QPen(Qt::red, 5));

    QVariantAnimation * anim = new QVariantAnimation(&scene);
    anim->setStartValue(QPointF(0, 100));
    anim->setEndValue(QPointF(100, 0));
    anim->setDuration(2000);
    anim->start();

    QObject::connect(anim, &QVariantAnimation::valueChanged, [item](const QVariant & val){
        QLineF l = item->line();
        l.setP2(val.toPointF());
        item->setLine(l);
    });

    view.resize(640, 480);
    view.show();
    return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM