gpt4 book ai didi

c++ - Qt中scenePos的QPropertyAnimation

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:46:49 25 4
gpt4 key购买 nike

我被场景中的简单属性动画的属性卡住了。该项目只需要移动一段距离。有了属性“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());
}

终于是动画的调用了。这个电话似乎没问题,因为它适用于new 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/

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