Qt 属性和QGraphicsSimpleTextItem

Qt 属性和QGraphicsSimpleTextItem,qt,properties,qgraphicssimpletextitem,Qt,Properties,Qgraphicssimpletextitem,我想知道我们是否可以在继承QGraphicsSimpleTextItem的类中使用属性来设置动画 我正在画这个按钮: 它由以下部分组成: 一个圆,它继承QGraphicsObject并替代几何体特性 椭圆,基本相同,但以圆为母 继承QObject和QGraphicsSimpleTextItem 对于前两个,动画可以工作。但关于最后一个问题,我有以下错误: QPropertyAnimation: you're trying to animate a non-existing property

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

我正在画这个按钮:

它由以下部分组成:

  • 一个圆,它继承QGraphicsObject并替代几何体特性

  • 椭圆,基本相同,但以圆为母

  • 继承QObjectQGraphicsSimpleTextItem

对于前两个,动画可以工作。但关于最后一个问题,我有以下错误:

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
这是我的课程“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;
};
还有我的

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);
}
我不知道是否可以命名我自己的属性,但我不能(?)重写
font
pos
属性,因为我继承了QGraphicsSimpleTextItem并使用
setFont()
setPos()

如果您想尝试,您可以找到所有代码

谢谢您的时间。

问题已解决

MyTextOk
类定义中的
Q_对象
宏丢失。放置后,代码运行良好


您可以找到我的按钮的一个工作示例。

我想您只是缺少了
MyTextOk
类定义中的
Q_对象
宏。虽然我很惊讶没有收到来自
moc
的任何警告。哦,我讨厌我的生活。真的!我没有更多的错误,但动画仍然无用。如果我有消息,我会回来的谢谢你,一切顺利。我们只需要调用pos和字体属性就可以了。这个Q_对象的疏忽让我产生了错误的想法。请将解决方案作为答案发布(确保链接中的相关内容包含在答案中),而不是作为问题的更新。这是为了帮助未来的游客,减少混乱。谢谢。完成了,谢谢你的建议。这比编辑更清楚你是对的。
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'

}