属性和 QGraphicsSimpleTextItem

Properties and QGraphicsSimpleTextItem

我想知道我们是否可以使用属性在继承 QGraphicsSimpleTextItem 的 class 中设置动画?

我正在绘制这个按钮:

它由 :

组成

对于前两个,动画有效。但是关于最后一个,我有以下错误:

QPropertyAnimation: you're trying to animate a non-existing property localisation of your QObject
QPropertyAnimation: you're trying to animate a non-existing property localisation of your QObject
QPropertyAnimation: you're trying to animate a non-existing property sizePolicy of your QObject
QPropertyAnimation: you're trying to animate a non-existing property sizePolicy of your QObject

这是我的 class 'MyText' :

class MyTextOk : public QObject, public QGraphicsSimpleTextItem
{
    Q_PROPERTY(QPointF localisation READ localisation WRITE setLocalisation)
    Q_PROPERTY(QFont sizePolicy READ sizePolicy WRITE setSizePolicy)
public:
    explicit MyTextOk(QGraphicsObject *parent = 0);
    ~MyTextOk();

    QPointF localisation() const;
    void setLocalisation(const QPointF &value);

    QFont sizePolicy() const;
    void setSizePolicy(const QFont &value);

private:
    QRectF boundingRect() const;

protected :
    QPointF point;
    QFont font;
};

还有我的.ccp

QVariant myFontInterpolator(const QFont &start, const QFont &end, qreal progress)
{
    if (progress<0.5)
    {
        int a = (1-progress)*50 + progress*45;
        QFont rt(start);
        rt.setPointSize(a);
        return rt;
    }
    else
    {
        int a = (1-progress)*45 + progress*50;
        QFont rt(start);
        rt.setPointSize(a);
        return rt;
    }
        Q_UNUSED(end)
}

MyTextOk::MyTextOk(QGraphicsObject *parent)
    : QObject(parent), QGraphicsSimpleTextItem(parent)
{
    point = QPointF(-40,-45);
    this->setText("Ok");
    this->setPos(point);
    this->setBrush(QBrush(Qt::white));
    font = QFont("Colibri",50);
    this->setFont(font);

    qRegisterAnimationInterpolator<QFont>(myFontInterpolator);
}

MyTextOk::~MyTextOk()
{

}

QPointF MyTextOk::localisation() const
{
    return point;
}

void MyTextOk::setLocalisation(const QPointF &value)
{
    if(point!=value)
    {
        point = value;
        update();
    }
}

QFont MyTextOk::sizePolicy() const
{
    return font;
}

void MyTextOk::setSizePolicy(const QFont &value)
{
    if(font!=value)
    {
        font=value;
        update();
    }
}

QRectF MyTextOk::boundingRect() const
{
    return QRectF(0,0,0,0);
}

然后在我的 MainWindow 中制作动画:

void MainWindow::lancerAnimBoutonRond()
{
    animationBoutonRondTaille = new QPropertyAnimation(roundButton, "geometry");

    animationBoutonRondTaille->setDuration(300);
    animationBoutonRondTaille->setKeyValueAt(0, QRectF(-90, -90, 180, 180));
    animationBoutonRondTaille->setKeyValueAt(0.5, QRectF(-85,-85,170,170));
    animationBoutonRondTaille->setKeyValueAt(1, QRectF(-90, -90, 180, 180));

    animationBoutonRondTaille -> start();

    animationBoutonRondEllipse = new QPropertyAnimation(whiteShadow, "geometry");

    animationBoutonRondEllipse->setDuration(300);
    animationBoutonRondEllipse->setKeyValueAt(0,QRectF(-70,-80,140,80));
    animationBoutonRondEllipse->setKeyValueAt(0.5,QRectF(-65,-75,130,90));
    animationBoutonRondEllipse->setKeyValueAt(1,QRectF(-70,-80,140,80));

    animationBoutonRondEllipse->start(); // These two work

    animationBoutonRondOk = new QPropertyAnimation(textOk,"localisation");

    animationBoutonRondOk->setDuration(300);
    animationBoutonRondOk->setKeyValueAt(0,QPointF(-40,-45));
    animationBoutonRondOk->setKeyValueAt(0.5,QPointF(-35, -40));
    animationBoutonRondOk->setKeyValueAt(1,QPointF(-40, -45));

    animationBoutonRondOk->start(); //error : QPropertyAnimation: you're trying to animate a non-existing property localisation of your QObject

    animationBoutonRondOkTaille = new QPropertyAnimation(textOk,"sizePolicy");

    animationBoutonRondOkTaille->setDuration(300);
    animationBoutonRondOkTaille->setStartValue(QFont("Colibri",50));
    animationBoutonRondOkTaille->setEndValue(QFont("Colibri",50));

    animationBoutonRondOkTaille->start();  //error : 'QPropertyAnimation: you're trying to animate a non-existing property sizePolicy of your QObject'

}

我不知道我是否可以命名我的 "own" 属性,但我不能(?)覆盖 fontpos 属性,因为我继承了 QGraphicsSimpleTextItem 并使用 setFont()setPos()

所有的代码都可以找到here如果你想试试

感谢您的宝贵时间。

问题已解决。

Q_OBJECT MyTextOk class 定义中的宏丢失。放置后,代码运行正常

您可以找到我的按钮的工作示例 here