C++ Qt C++;QGraphicsItem在调用advance()时将位置更改为(2x,2y) QGraphicsItem在调用advance()时将位置更改为(2x,2y) 问题

C++ Qt C++;QGraphicsItem在调用advance()时将位置更改为(2x,2y) QGraphicsItem在调用advance()时将位置更改为(2x,2y) 问题,c++,qt,qt-creator,qgraphicsitem,qgraphicsscene,C++,Qt,Qt Creator,Qgraphicsitem,Qgraphicsscene,我有一个扩展QGraphicsItem的类,在调用advance()为对象设置动画之前,对象在场景中显示良好。一旦调用advance并更改位置,对象将立即从(x,y)移动到(2x,2y)。这是我的对象代码 班级 创建时,对象在(x,y)处正确显示,但只要调用advance,对象就会移动到(2x,2y)并从此处设置动画 我有另一个结构类似的类,它面临着类似的问题 到目前为止,我提出的唯一可能的解决方案是将坐标除以2,并在构造函数中设置位置,但是这仍然不允许与场景中的其他对象进行任何比较(例如包含)

我有一个扩展QGraphicsItem的类,在调用advance()为对象设置动画之前,对象在场景中显示良好。一旦调用advance并更改位置,对象将立即从(x,y)移动到(2x,2y)。这是我的对象代码

班级 创建时,对象在(x,y)处正确显示,但只要调用advance,对象就会移动到(2x,2y)并从此处设置动画

我有另一个结构类似的类,它面临着类似的问题

到目前为止,我提出的唯一可能的解决方案是将坐标除以2,并在构造函数中设置位置,但是这仍然不允许与场景中的其他对象进行任何比较(例如包含)

另外,下面是我用来将对象添加到场景中的代码

呼叫码
int column\u width=scene->width()/width;
int column_height=scene->height()/height;
int random=qrand()%高度;
QRectF rect(场景->宽度(),随机*列高度,列宽度,列高度);
追加(新僵尸(rect,refreshRate));
场景->添加项(僵尸在(0));

setX()不能修改x吗?为什么要修改x成员,然后调用setX?oO@Félixcanturnet我认为更改x成员只是调整局部值,而setX()是修改打印位置。尽管如此,我确实同意这是有道理的。但是,当我尝试这种方法时,我被迫重写抽象成员函数boundingRect,并且值仍然能够加倍。如果我打印这些值,它们甚至会被保留为您所期望的值的1/2。谢谢
Zombie::Zombie(QRectF bounds, int refreshTime){

    speed = 5.0/1000*refreshTime;
    image = ":/assets/RegularZombie.png";
    x = bounds.x();
    y = bounds.y();
    width = bounds.width();
    height = bounds.height();

}

void Zombie::advance(int phase){

    if (!phase || eating) return;

    x = x-speed;

    //setPos(boundingRect().topLeft());
    setX(x);

}
QRectF Zombie::boundingRect() const{
    int x_pos = (int)x;
    return QRectF(x_pos,y,width,height);
}
void Zombie::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->drawImage(boundingRect(),QImage(image));
}
int column_width = scene->width()/WIDTH;
int column_height = scene->height()/HEIGHT;
int random = qrand()%HEIGHT;

QRectF rect(scene->width(),random*column_height,column_width,column_height);
zombies.append(new Zombie(rect,refreshRate));

scene->addItem(zombies.at(0));
cout<<rect3.x()<<endl;
cout<<zombies.at(0)->boundingRect().x()<<endl;

scene->update();