QGraphicsItem::itemChange() may do what you want:

http://doc.qt.nokia.com/4.7-snapshot/qgraphicsitem.html#itemChange

Martin.

From: qt-qml-boun...@trolltech.com [mailto:qt-qml-boun...@trolltech.com] On 
Behalf Of ext Charley Bay
Sent: Tuesday, 23 November 2010 12:51 PM
To: qt-qml@trolltech.com
Subject: [Qt-qml] C++ MyClass to detect child added/removed from QML

I'm sure the answer is simple, but I can't find it:  I want my C++ implemented 
QML object to be notified when children are added:

//FILE: MyFile.qml
import Qt 4.7
import MyCppClass 1.0

Rectangle {
  MyCppClass {
    id: myCppClass
    Text { text: "some text" }   // CHILD NOT DETECTED AS ADDED
  }

  Text {
    text: "some other text"
    parent:  myCppClass  // CHILD NOT DETECTED AS ADDED
  }
}

In both the cases above, I'm unable to find a slot/notification/virtual 
function in the C++ implementation of MyCppClass that I can use to detect these 
children that are added.

Since MyCppClass derives from QDeclarativeItem, I see that it is a 
QGraphicsObject, mulitply-derived from from both QObject and QGraphicsItem.  I 
stumbled onto "virtual QObject::childEvent(QChildEvent*)" which looked ideal 
(since QChildEvent has child add/remove info), but it's not called in either of 
the cases above.

Then, digging into the Qt docs, I see that QGraphicsObject users should *NOT* 
look to QObject for parent/children relationships, but rather, should look only 
to QGraphicsItem.  Seems reasonable.  Unfortunately, I could not find anything 
in QGraphicsItem that looked like a slot or virtual function I could override 
to receive "add/remove child" notification events (I need both).

I'm sure this is a simple answer -- what notification does QML trigger that I 
can catch on the C++ side for (re-)parenting?

Thanks!

--charley
_______________________________________________
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to