O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Keyboard focus in graphics scene

The scene implements the concept of focus that works similar to keyboard focus in widgets. Only one item can have focus at a time. When the scene receives a keyboard event, it is dispatched to the focus item.

To be focusable, an item must have the QGraphicsItem::ItemIsFocusable flag enabled:

item1->setFlag(QGraphicsItem::ItemIsFocusable, true);item2->setFlag(QGraphicsItem::ItemIsFocusable, true);

Then, an item can be focused by a mouse click. You can also change the focused item from the code:

item1->setFocus();

Another way to set the focus is to use the scene's QGraphicsScene::setFocusItem() function, which expects a pointer to the item you like to focus as a parameter. Every time an item gains focus, the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required