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);