Explicitly Naming QGraphicsItem Objects

Last edited on

Overview

For Squish it is technically impossible to reliably check whether a QGraphicsItem object is also a QObject. So Squish has no way to find out whether a custom class extends QGraphicsItem as well as QObject, which means that Squish has no access to the meta object system provided by QObject.

Objects based on QGraphicsItem

QGraphicsItem does not have a naming mechanism.

QGraphicsItem does not have a meta object system, therefore you cannot add a Qt property to a QGraphicsItem sub-class to access that property for name generation.

Objects based on QGraphicsItem and QObject

QGraphicsItem does not have a meta object system, therefore Squish cannot check if an object extends QObject or not. This means that Squish does not know if it can safely cast an object to a QObject, because if it does that, to access the meta object system of QObject, this may cause a crash of the automated application in case the object is not a sub-class of QObject.

This also means that Squish cannot determine the class name of the object to decide whether it is safe to cast to a QObject.

Solution

The only alternative is to use QGraphicsObject instead of using a sub-class that extends QGraphicsItem and QObject.

Multiple Inheritance from QGraphicsObject and concrete QGraphics*item

If you already use a concrete QGraphicsItem type and want to add an objectName visible from Squish, it makes sense to use multiple inheritance from QGraphicsObject and the concrete type. However, this means you will indirectly multiply-inherit from QGraphicsItem. You must make sure all virtual functions are properly overridden to do the right thing(s), and further, make sure that scene->addItem() gets a QGraphicsObject* as its argument, to tell both Qt and Squish that this class has a proper objectName.

Attached to this page is a diff that shows how to modify our examples/qt/Shapes example so that the RegularPolygonItem uses multiple inheritance in this way.

Related Information