Ah, figured out a solution - qgraphicsitem_cast returns a zero if the cast doesn't work, so can try and recast it using the other class type if that's the case.