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.

Another useful drag ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.