I'm writing an application that uses QGraphicsScene and subclassed QGraphicsItems, based on the Diagram Scene demo app. The user can click on the scene and select an item, which can be either a DiagramItem (which inherits QGraphicsPolygonItem), or an Arrow (which inherits QGraphicsLineItem).
I need to distinguish the class that the selected object belongs to, but I'm not sure how to do it. Below is the relevant code from my subclassed QGraphicsScene class (DiagramScene):
{
if (itemAt(event->scenePos())) {
DiagramItem *selectedItem = qgraphicsitem_cast<DiagramItem *>(itemAt(event->scenePos()));
// operate on selectedItem
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (itemAt(event->scenePos())) {
DiagramItem *selectedItem = qgraphicsitem_cast<DiagramItem *>(itemAt(event->scenePos()));
// operate on selectedItem
To copy to clipboard, switch view to plain text mode
How can I modify this to check whether I've selected a DiagramItem or an Arrow, and then process selectedItem accordingly? At the moment the app crashes when I click on an Arrow object. (I thought about using exception handling, but am not sure how you try and catch casting errors.)
Any advice would be greatly appreciated!
Bookmarks