我陷入了一个场景中的简单Propertyanimation的属性问题。该项目仅需移动一定距离。使用属性“pos”,它将移动。对于我的媒体资源“ScenePosition”,它不起作用。调试器进入函数setScenePosition(),但它对显示的场景没有影响。
// chip.h
class Chip : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
//THIS WORKS - But i have to use scenePosition instead because of the scene
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
// MY ATTEMPT
Q_PROPERTY(QPointF ScenePosition READ ScenePosition WRITE setScenePosition)
public:
explicit Chip(int x, int y, int w, int h, QObject *parent=NULL);
void setScenePosition(QPointF p);
QPointF ScenePosition();
};
我想我以错误的方式使用了scenePos()。
//chip.cpp
void Chip::setScenePosition(QPointF p)
{
this->scenePos().setX(p.x());
this->scenePos().setY(p.y());
}
最后调用动画。该 call 似乎还不错,因为它可以与
新的QPropertyAnimation(item,“pos”)但不包含“ScenePosition”,这使我对setter的实现产生了疑问。
item = new Chip(col*wCol, 100, wCol, wCol);
item->setVisible(true);
QPropertyAnimation *animation = new QPropertyAnimation(item, "ScenePosition");
addItem(item);
animation->setDuration(1000);
animation->setEndValue( QPoint(col*wCol, 400 - wCol*stacked) );
animation->setEasingCurve(QEasingCurve::OutElastic);
animation->start();
最佳答案
this->scenePos()
返回QPointF
。使用this->scenePos().setX(p.x());
仅会影响临时QPointF
对象,显然不会影响项目位置。您将需要使用this->setScenePos(p);
之类的东西。但是,没有这种方法。如果该项没有父项,则this->setPos(p)
将等效地工作。如果项目具有父项目,则可以使用this->setPos(this->parentItem()->mapfromScene(p));
。 mapFromScene
将p
从场景坐标转换为父项坐标系,并且子项使用父项坐标系定位,因此它应该可以正常工作。
关于c++ - Qt中的ScenePos的QPropertyAnimation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21047040/