Time for action – using properties, signals, and slots with items

So let's alter the Player class to use QObject:

class Player : public QObject, public QGraphicsPixmapItem {
  Q_OBJECT

All you have to do is to add QObject as a base class and add the Q_OBJECT macro. Now you can use signals and slots with items too. Be aware that QObject must be the first base class of an item.

Tip

If you want an item that inherits from QObject and QGraphicsItem, you can directly inherit QGraphicsObject. Moreover, this class defines and emits some useful signals such as xChanged() when the x coordinate of the item has changed or scaleChanged() when the item is scaled.

Note

A word of warning: Only use QObject with items if you really need its capabilities. QObject adds a ...

Get Game Programming Using Qt 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.