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 lot of overhead to the item, which will have a noticeable impact on performance when you have many items. So use it wisely and not only because you can.
Let us go back to our player item. After adding QObject
, we define a property called m_jumpFactor...