QGraphicsItem subclass and accessing custom properties
I created several subclasses of QGraphicsItem's and added them to the scene.
How can I access their custom properties added by me using [...] ?
Code:
scene.itemAt(x_number).myproperty
If I try creating a new placeholder subclass instance, and assigning scene.itemAt(x) to it, I receive an error: "QGraphicsItem is private"
Re: QGraphicsItem subclass and accessing custom properties
Quote:
Originally Posted by
been_1990
If I try creating a new placeholder subclass instance, and assigning scene.itemAt(x) to it, I receive an error: "QGraphicsItem is private"
Make it public?
Re: QGraphicsItem subclass and accessing custom properties
My problem is similar to here.
In the above thread, Wysota says:
Quote:
casting that listbox item to your class
And that was the solution, but I have no idea how to do that.
In my case it would be casting a QGraphicsScene item to my subclass.
Re: QGraphicsItem subclass and accessing custom properties
Hi,
itemAt() returns a pointer to a QGraphicsItem. So the returned value is a (QGraphicsItem *) and not a (YourSubClass *). If the return object is your own subclass (and not some other object that is also in the graphics scene) and you want to use its specific methods, you must first cast it to your own class.
See int QGraphicsItem::type () and T qgraphicsitem_cast ( QGraphicsItem * item ) in the documentation for a possible solution. It is used in the diagramscene example with Qt.
Regards,
Marc
Re: QGraphicsItem subclass and accessing custom properties
Ok, thanks Marc I went straight to the Docs and was able to work it out. :)