I have written code in my implemented QGraphicsScene to cast into a custom class from what QGraphicsScene::itemAt
returns. Interestingly, qgraphicsitem_cast
always returns zero, but using dynamic_cast
works fine. Why is this?
void VScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// ...
QGraphicsItem *item = itemAt(event->scenePos(), QTransform());
if (item->type() == TypeVModuleItem) {
// This returns 0
VModule *module = qgraphicsitem_cast<VModule *>(item);
// This works
module = dynamic_cast<VModule *>(item);
// ...
}
}
It is worth mentioning that VModule is a QGraphicsPolygonItem, not a QGraphicsItem:
VModule::VModule(QGraphicsItem *parent = 0) : QGraphicsPolygonItem(parent)
{
// ...
}
Here is the type implementation in VModule:
int type() const { return TypeVModuleItem; }
Does this have any bearing on the failure of the cast? Are there any consequences to using dynamic_cast
in this way, such as losing data, that I am not aware of? Thanks in advance for any advice.