Time for action – Reacting to an item's selection state

Standard items, when selected, change appearance (for example, the outline usually becomes dashed). When we're creating a custom item, we need to implement this feature manually. Let's make our item selectable in the View constructor:

SineItem *item = new SineItem();
item->setFlag(QGraphicsItem::ItemIsSelectable);

Now, let's make the graph line green when the item is selected:

if (option->state & QStyle::State_Selected) {
    pen.setColor(Qt::green);
}
painter->setPen(pen);

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.