PDA

View Full Version : QGraphicsItem subclass and accessing custom properties



been_1990
18th November 2010, 03:21
I created several subclasses of QGraphicsItem's and added them to the scene.
How can I access their custom properties added by me using [...] ?
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"

tbscope
18th November 2010, 07:55
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?

been_1990
18th November 2010, 20:24
My problem is similar to here (http://www.qtcentre.org/threads/514-accessing-subclass-of-QListBoxText).
In the above thread, Wysota says:

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.

marcvanriet
18th November 2010, 21:49
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

been_1990
19th November 2010, 02:48
Ok, thanks Marc I went straight to the Docs and was able to work it out. :)