QGraphicsScene::addItem: 项目已经被添加到这个场景

QGraphicsScene::addItem: item has already been added to this scene

我尝试向 QGraphicsScene 添加多个项目,但在应用程序输出中调用 scene->addItem(new Bonus(Bonus::BonusType::coin, randPoint, pixels, parent)); 后出现此消息:QGraphicsScene::addItem: item has already been added to this scene
我做错了什么?
代码:

for(int i = 0; i < coinsCount; ) {
    QPoint randPoint(random() % g->getWidth(),
                     random() % g->getHeight());

    if(g->getType(randPoint) != Graph::wall && !usedPoints.contains(randPoint)) {
        scene->addItem(new Bonus(Bonus::BonusType::coin, randPoint, pixels, parent));
        usedPoints.push_back(randPoint);
        i++;
    }
}

您传递的是父项。如果父项已经添加到场景中,将其设置为新项的父项也会将后者添加到场景中。

构造函数在 addItem() 之前运行,因此在执行后者时,项目已经在场景中。

Note that this implicitly adds this graphics item to the scene of the parent. You should not add the item to the scene yourself.