Doc: Let SQL Drill Down example show correct use of qgraphicsitem_cast

For qgraphicsitem_cast to work correctly with custom items, one must
reimplement the type() function for each custom QGraphicsItem subclass.
Do that in the example code.

Fixes: QTBUG-7357
Change-Id: Ibf221817908355f0347cddfc46747b6955958f5f
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
This commit is contained in:
Paul Wicking 2020-03-06 07:29:12 +01:00
parent c3ae30085e
commit a2446c0eed
2 changed files with 6 additions and 0 deletions

View File

@ -437,6 +437,9 @@
\snippet drilldown/imageitem.h 0 \snippet drilldown/imageitem.h 0
We declare a \c Type enum value for our custom item and reimplement
\l{QGreaphicsItem::}{type()}. This is done so we can safely use
qgraphicsitem_cast().
In addition, we implement a public \c id() function to be able to In addition, we implement a public \c id() function to be able to
identify the associated location and a public \c adjust() function identify the associated location and a public \c adjust() function
that can be called to ensure that the image item is given the that can be called to ensure that the image item is given the

View File

@ -60,8 +60,11 @@ class ImageItem : public QObject, public QGraphicsPixmapItem
Q_OBJECT Q_OBJECT
public: public:
enum { Type = UserType + 1 };
ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = nullptr); ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
int type() const override { return Type; }
void adjust(); void adjust();
int id() const; int id() const;