gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:09:40 30 4
gpt4 key购买 nike

我从 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;

最佳答案

您的属性声明“存在名为 size() 和 setSize() 的函数”。但是你的类(class)里没有他们。

关于c++ - 如何为 GraphicsItem 创建自定义属性并在 QPropertyAnimation 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43776411/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com