I have the problem that i want to set a Q_PROPERTY macro for my own class inherits by QGraphicsPixmapItem. So i my first property where i set the position works very well, but the second one where i want to set a rotation for the Qt::YAxis does not work and i dont know how to write it correctly. I need this propertys for an animation. Here is what im trying:
–A.h–
class myOwnPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY (QPointF pos READ pos WRITE setPos)
Q_PROPERTY (QTransform rot READ rot WRITE setRotation)
public:
flowNpixmapItem()
};
–A.cpp–
myItemPosAnim -> setTargetObject (myItem);
myItemTransAnim -> setTargetObject (myItem);
myItemPosAnim -> setStartValue (QPointF (cover0 -> pos()));
myItemTransAnim -> setStartValue (QTransform::rotate (0, Qt::YAxis));
The problem is that
QTransform::rotateis a non-static member function, meaning that this syntax can only be used to refer a function (function pointer), not to call the function.The method
QPropertyAnimation::setStartValuetakes a value (not a function) as its argument, so in your case a QTransform value.QTransform::rotate(...)(with arguments) is no value, indeed, it can’t be compiled because the compiler thinks you want to call the static method with the given arguments.So you probably want this:
But I still see a big problem in your code: You animate a transform, not the rotation itself. Rotation transformation is non-linear, meaning that if you try to animate a transformation from 0° to 180°, you would not get what you want. You would rather get a scaling animation, because 180° rotation equals scaling the coordinate system by factor -1. This is because scaling is a linear transformation, and QPropertyAnimation interpolates between the start and end value, so “the middle between start and end” is calculated using
0.5 * start + 0.5 * end, which is not what you want for rotation matrices.Since the rotation of QGraphicsItems is of type
qrealanyway, just use this type:You should then use this like this: