Python & Qt、QGraphicsScene 碰撞和 typeid(来自 c++)

Python & Qt, QGraphicsScene Collision and typeid (from c++)

我很难找到关于这个主题的帮助。

我成功地进行了碰撞检测,但现在我想检测碰撞对象的类型。 现在是 QGraphicsPixmapItem。

我已经用 C++ 编写了代码,但现在 python 不知道 typeid 我不知道该怎么做。

Python 函数:

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

有效的 C++ 等效代码:

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

基础 class QGraphicsItem 有一个“type”方法,return是一个“int”,正是为了这个目的。它应该比使用 typeid 或任何涉及字符串查找或比较的方法更有效。每个内置 classes 派生的 QGraphicsItem 都有一个独特的类型,当你派生你自己的 classes 时,只需在你的 class 和 [=17] 中实现“类型”方法=] 唯一值。

在文档中查找 QGraphicsItem::UserType。那里有一个例子。 Qt 为自己保留最大 65535 的值,用户类型从 65536 开始。