我试图将几个项目添加到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()
之前运行,因此在执行ojit_code时,该项目已在场景中。