There is something weird with QGraphicsScene::removeItem
, when I remove two items from the scene I get the following message:
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)
Even though the two items are in the scene.
I've checked the items scene against the scene but it returns true:
if(Player->scene() == m_scene){
m_scene->addText("Same"); // this text is added
}else{
m_scene->addText("Different");
}
m_scene
come from this class:
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;
};
And the class Character
is a class inheriting from 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;
};
And this is where I call 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();
}
}
Can someone explain me why I get these messages, please?
EDIT:
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);
}
Finally I've found it.
It was in
Game::FixedUpdate()
function, it'sPlayer->collidesWithItem(enemy)
beingtrue
even after the pointers were removed from the scene. So I've put a check before it checks for collision, as so:Which does not give the messages after removal.