gpt4 book ai didi

c++ - QGraphicsScene::removeItem: item场景和这个场景不一样

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:00 31 4
gpt4 key购买 nike

QGraphicsScene::removeItem 有一些奇怪的地方,当我从场景中删除两个项目时,我收到以下消息:

QGraphicsScene::removeItem: item 0x5edb28's scene (0x0) is different from this scene (0x5e7790)
QGraphicsScene::removeItem: item 0x5edc18's scene (0x0) is different from this scene (0x5e7790)

即使这两个项目在场景中。

我已经根据场景检查了项目场景,但它返回 true:

if(Player->scene() == m_scene){
m_scene->addText("Same"); // this text is added
}else{
m_scene->addText("Different");
}

m_scene 来自这个类:

class Game : public QGraphicsView
{
Q_OBJECT

public:
Game();

public slots:
void FixedUpdate();

signals:


private:
QRectF *sceneSize;
QGraphicsScene *m_scene;
QGraphicsRectItem *m_rect;
QTimer *m_timer;
Character *Player;
Character *enemy;

};

Character类是继承自QGraphicsPolygonItem的类:

class Character : public QObject, public QGraphicsPolygonItem
{
Q_OBJECT

public:
Character(qreal x, qreal y, qreal w, qreal h, QString tag, QGraphicsScene *scene = 0, QGraphicsItem *parent = 0);
Character(QRectF rectF, QString tag, QGraphicsScene *scene = 0, QGraphicsItem *parent = 0);
Character(QString tag, QGraphicsScene *scene = 0, QGraphicsItem *parent = 0);
void setValue(qreal x, qreal y, qreal w, qreal h);
void setVelocity(qreal vx, qreal vy);
void setVelocityX(qreal vx);
void setVelocityY(qreal vy);
void setTag(QString tag);
qreal vx() const;
qreal vy() const;
qreal w() const;
qreal h() const;
QString tag() const;
QGraphicsScene *currentScene() const;
void fixedUpdate();
void removeCharacter();

signals:


public slots:


private:
qreal m_x;
qreal m_y;
qreal m_w;
qreal m_h;
qreal m_vx;
qreal m_vy;
QString m_tag;
QGraphicsScene *m_scene;
};

这就是我调用 removeItem

的地方
void Game::FixedUpdate(){
if(Player->collidesWithItem(enemy)){
Player->setVelocityX(0);
enemy->setVelocityX(0);

//removeCharacter just call scene()->removeItem(this);
Player->removeCharacter();
enemy->removeCharacter();
}else{
Player->fixedUpdate();
enemy->fixedUpdate();

}
}

有人可以解释一下为什么我会收到这些消息吗?

编辑:

Game::Game(){
sceneSize = new QRectF(0, 0, 640, 480);
m_scene = new QGraphicsScene(*sceneSize, this);
setScene(m_scene);
Player = new Character(QRectF(0, 0, 50, 50), "player", m_scene);
Player->setVelocityX(1.5);
//m_scene->addItem(Player);

enemy = new Character(QRectF(500, 0, 20, 20), "enemy", m_scene);
enemy->setVelocityX(-0.5);
//m_scene->addItem(enemy);

//m_scene->addText(QString::number(Player->x() + Player->vx()));

if(Player->scene() == m_scene){
m_scene->addText("Same");
}else{
m_scene->addText("Different");
}

m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(FixedUpdate()));
m_timer->start(1000 / 60);
}

最佳答案

终于找到了。

它在 Game::FixedUpdate() 函数中,它是 Player->collidesWithItem(enemy)true 即使在指针被删除之后从现场移除。所以我在检查碰撞之前进行了检查,如下所示:

void Game::FixedUpdate(){
if(Player->scene() != NULL && enemy->scene() != NULL){
if(Player->collidesWithItem(enemy)){
Player->setVelocityX(0);
enemy->setVelocityX(0);

//removeCharacter just call scene()->removeItem(this);
Player->removeCharacter();
enemy->removeCharacter();
}else{
Player->fixedUpdate();
enemy->fixedUpdate();

}
}
}

删除后不显示消息。

关于c++ - QGraphicsScene::removeItem: item场景和这个场景不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864894/

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