PDA

View Full Version : Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems()



Durkin
13th June 2013, 20:05
Hello,

This might be a c++ question rather than qt.

I have written a custom class which inherits from QGraphicsPolygonItem and have populated a scene with this kind of object. Now I wish to know when one of them is selected, so I 've connected the scene's selectionChanged() signal to a custom slot. In that slot function, the scene's selectedItems() will give me a QList<QGraphicsItem*>, ie a list of the items selected. However, I want a qlist of my custom class.
From what I read on casting base class to derived, dynamic_cast could only work if QGraphicsItem was polymorphic. Trying static_cast instead caused a seg fault. Am I right to assume that the QList returned by selectedItems() contains what I want and it is a matter of casting? Should I be approaching this in an entirely different manner? (although I do not want to turn my custom class into a QObject to use its signals)

Any advice or ideas would be appreciated.

anda_skoa
13th June 2013, 20:54
dynamic_cast should work, it is there for exactly that purpose.

It will return the pointer casted to your class or 0 if the object is not of the cast type.

Cheers,
_

Durkin
13th June 2013, 21:52
You 're right, it does. The seg fault was my mistake.

Santosh Reddy
14th June 2013, 04:59
If you have implemented the QGraphicsItem:type() in your custom class, then you can directly use the type() to determine the custom items, it should be more simpler and quicker than using dynamic_cast.