In the function of itemChange, first ,I get the child item that will be added, then I use dynamic_cast cast it to ‘MyItem’, but the cast always fail.
QVariant MyItem::itemChange ( GraphicsItemChange change, const QVariant & value )
{
if (change==ItemChildAddedChange)
{
QGraphicsItem* item=value.value<QGraphicsItem*>();
if (item)
{
MyItem* myItem=dynamic_cast<MyItem*>(item);//myItem always be NULL,
//although I know the item is 'MyItem' type.
if (myItem)
{
qDebug()<<"successful!";
}
}
}
return QGraphicsItem::itemChange(change,value);
}
Thanks very much!
Note the comment on
itemChange:dynamic_castcan also fail if the object is not fully constructed. (I don’t quite understand the spec on this, but there are some cases where it will, some where it will not.) If you set the parent after constructing the item, it will work: