PDA

View Full Version : identify QGraphicItem



avis_phoenix
26th August 2007, 17:25
I have a class of QGraphicItem type that contains special properties and I wanted that when selected one of them, it knew as he is the one that they selected but not like QGraphicItem but like an object of the class which I did, somebody knows like doing this?
Thanks

darksaga
26th August 2007, 18:01
how about:



MyItem *item = NULL;
item = qobject_cast<MyItem *>(selectedItem());
if (item != NULL){
item->doSomething();
}

Gopala Krishna
26th August 2007, 18:53
qobject_cast works only if the item also inherits QObject which the default QGraphicsItems don't inherit.
So my advice is to use qgraphicsitem_cast instead (replace qobject_cast with qgraphics_cast in above example). Read this qt doc (http://doc.trolltech.com/4.3/qgraphicsitem.html#qgraphicsitem_cast).

Bitto
27th August 2007, 13:17
Or just use standard C++ dynamic_cast<T>(). qobject_cast and qgraphicsitem_cast are only necessary for apps that are compiled with RTTI disabled; mostly for embedded apps, or for old compilers. For modern apps, I just use dynamic_cast ;-).

Note: qgraphicsitem_cast does not support polymorphic casts.

avis_phoenix
12th September 2007, 06:36
Well when it tries to use qgraphicsitem_cast I could not, then I wanted some tutorial or example to using it :o , Thanks.

Sorry for my bad english.

avis_phoenix
24th September 2007, 07:16
Well... here the correct code for use qgraphicsitem_cast:



Eitem = NULL;
foreach (QGraphicsItem *item, Scene->selectedItems())
{
Eitem = qgraphicsitem_cast<MyClass*>(item);
if (Eitem != NULL)
{
//My Code
}
}



Thanks for all :)