QGraphicsScene :: removeItem:项目场景与此场景不同

时间:2014-11-11 12:25:41

标签: c++ qt qgraphicsview qgraphicsscene

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);
}

1 个答案:

答案 0 :(得分:0)

最后我找到了它。

它位于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();

        }
    }
}

删除后不会给出消息。