[Qt-qml] C++ MyClass to detect child added/removed from QML

Charley Bay charleyb123 at gmail.com
Tue Nov 23 03:50:51 CET 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20101122/9b135dd2/attachment.html 


More information about the Qt-qml mailing list