如何为GraphicsItem创建自定义属性并在QPropertyAnimation中使用它

时间:2017-05-04 07:22:14

标签: c++ qt4 qt5

我从QGraphicsItem派生自定义项,并为QPropertyAnimation

添加了一些属性
        Q_PROPERTY(qreal x READ x WRITE setX);
        Q_PROPERTY(qreal y READ y WRITE setY);
        Q_PROPERTY(QPointF pos READ pos WRITE setPos);

这是我的头文件

    #ifndef MYSQUARE_H
    #define MYSQUARE_H
    #include<QObject>
    #include<QPainter>
    #include<QGraphicsItem>
    #include<QGraphicsItemGroup>
    #include<QGraphicsPixmapItem>
    #include<QPainterPath>
    #include<QPointF>
    #include<QSizeF>
    #include<QImage>
    #include<QLabel>
    #include<QCoreApplication>
    #include <QStringBuilder>
    #include <QString>
    #include <QTranslator>
    #include <QList>
    #include <QVariant>
    #include <QDebug>
    #include <QtSvg>
    #include <QSvgGenerator>
    #include "arrow.h"
    #include "mysvgitem.h"
    class QGraphicsSvgItem;
    class QPolygonF;
    class Arrow;

    class MySquare : public QObject,  public QGraphicsItem
    {
        Q_OBJECT
        Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity);
        Q_PROPERTY(QPointF pos READ pos WRITE setPos);
        Q_PROPERTY(qreal x READ x WRITE setX);
        Q_PROPERTY(qreal y READ y WRITE setY);
        enum resize_direction
        {
            rd_none,
            rd_left,
            rd_top,
            rd_right,
            rd_bottom
                                                                                                                                      1,1           Top
        };
    public:
        MySquare();

        QRectF boundingRect() const;
        QPolygonF polygon() const { return myPolygon; }
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
        bool Pressed;
        void addArrow(Arrow *arrow);
        QPointF returnCenter();
        void setMyPolygon();
        void setItemSize(qreal height,qreal width);
        void setImage(const QString& str);
        void setImage(const QString& str ,QSize);
        void setImage(const QString& str ,QRectF rec);
        void setSvgImage(const QString& str);
        void setSvgImage(const QString& str ,QSizeF);
        void setSvgImage(const QString& str ,QRectF rec);
        void setText(const QString& str ,QRectF rec);
        ~MySquare();
    signals:
        void click();

    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
        QVariant itemChange(GraphicsItemChange change, const QVariant &value) ;
    private:
        QPolygonF myPolygon;
        QList<Arrow *> arrows;
        QRectF myItemRect;
        QString MyText;
        QRectF MyTextRect;
        QGraphicsTextItem *itemXY;
        QString *posXY;
        resize_direction resize_direction_;


    };

    #endif // MYSQUARE_H

特别是我想创建属性以在动画中使用它,怎么可能这样做。

我尝试使用像size这样的大小属性        Q_PROPERTY(QSize size READ size WRITE setSize);

但我收到错误

   moc_mysquare.cpp: In member function ‘virtual int   MySquare::qt_metacall(QMetaObject::Call, int, void**)’:
   moc_mysquare.cpp:138:55: error: ‘size’ was not declared in this scope
             case 1: *reinterpret_cast< QSize*>(_v) = size(); break;
                                                   ^
   moc_mysquare.cpp:149:55: error: ‘setSize’ was not declared in this scope
         case 1: setSize(*reinterpret_cast< QSize*>(_v)); break;

1 个答案:

答案 0 :(得分:1)

您的属性声明“有名为size()和setSize()的函数”。但是你的课堂上没有它们。