Issue
I'm having big trouble finding help on this subject.
I managed to have a collision detection working but now I want to detect the type of the colliding object. It's a QGraphicsPixmapItem for now.
I've already coded that in c++ but now that python don't know about typeid I don't know what to do.
Python func:
def IsColliding(self, charView):
collision_list = self.Map_Scene.collidingItems(charView, mode=QtCore.Qt.IntersectsItemShape)
if collision_list:
print(collision_list)
print(typeid(collision_list[0])) ## Error here
return True
else:
return False
Working c++ equivalent code:
void xGame::placeBlock(int xpos, int ypos, QString blockName, bool isObs) {
//place new block
block = new xBlock(blockName, isObs);
block->setPos(xpos, ypos);
block->setZValue(1);
scene->addItem(block);
//remove old block
QList<QGraphicsItem *> colliding_items = block->collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; ++i) {
if (typeid(*(colliding_items[i])) == typeid(xBlock)) {
scene->removeItem(colliding_items[i]);
delete colliding_items[i];
}
}
}
Solution
The base class QGraphicsItem has a "type" method that returns an "int" and is exactly for this purpose. It should be a lot more efficient than using typeid or anything that involves string lookups or comparisons. Each of the built-in classes derived QGraphicsItem have a unique type, and when you derive your own classes, just implement the "type" method in your class and return a unique value.
Look in the docs for QGraphicsItem::UserType. There's an example there. Qt reserves values up to 65535 for itself, and user types start at 65536.
Answered By - goug
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.