Reputation: 193
I derive custom item from QGraphicsItem
and added some properties for 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);
this is my header file
#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
Especially I want to create property to use it in animation, how is it possible to do.
I tried to use size property like Q_PROPERTY(QSize size READ size WRITE setSize);
but I receive error
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;
Upvotes: 0
Views: 114
Reputation: 1686
Your property states that "there are functions named size() and setSize()". But you don't have them in your class.
Upvotes: 1