Item selection
The scene supports the ability of selecting items, similar to how you select files in a file manager. To be selectable, an item must have the QGraphicsItem::ItemIsSelectable flag turned on. Try to add parent->setFlag(QGraphicsItem::ItemIsSelectable, true) to the createComplexItem() function we created earlier. Now, if you run the application and click on a rectangle, it is selected, which is indicated by dashed lines:
You can use the Ctrl button to select multiple items at once. Alternatively, you can call view.setDragMode(QGraphicsView::RubberBandDrag) to activate the rubber band selection for the view.
Besides that, there are different ways to select items programmatically. There is the item's QGraphicsItem::setSelected() function, which takes a bool value to toggle the selection state on or off, or you can call QGraphicsScene::setSelectionArea() on the scene, which takes a QPainterPath parameter as an argument, in which case all items within the area are selected.
With the scene's QGraphicsScene::selectedItems() function, you can query the actual selected items. The function returns a QList holding QGraphicsItem pointers to the selected items. For example, calling QList::count() on that list will give you the number of selected items. To clear the selection, call QGraphicsScene::clearSelection(). To query the selection state of an item, use QGraphicsItem::isSelected(), which returns true if the item is selected and false otherwise.