属性和QGraphicsSimpleTextItem

时间:2017-06-29 15:35:46

标签: qt properties qgraphicssimpletextitem

我想知道我们是否可以在继承 QGraphicsSimpleTextItem 的类中使用属​​性进行动画处理?

我正在画这个按钮:

它由以下内容组成:

  • 一个圆圈,它继承了QGraphicsObject并覆盖了几何属性

  • 椭圆,基本相同,但将圆圈视为父级

  • 继承 QObject QGraphicsSimpleTextItem

  • 的文字

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

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;
};

我的.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'

}

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

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

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

问题解决了。

缺少Q_OBJECT类定义中的

MyTextOk宏。放置后,代码运行正常。

您可以找到我的按钮here的实际示例。