[Kde-bindings] Using ChildAdded and ChildRemoved event types to watch QObject parent changes
Richard Dale
rdale at foton.es
Fri Jun 8 09:47:20 UTC 2007
I've got distracted from writing up Qyoto docs because I've been looking at
the QtJambi code now it's released. One of the things I've found is how it
uses QChildEvents to tell when a parent has been added to or removed from a
QObject instance. Here is the relevant code from qtjambi/qtjambi_core.cpp:
void qtjambi_register_callbacks()
{
QInternal::registerCallback(QInternal::ConnectCallback,
qtjambi_connect_callback);
QInternal::registerCallback(QInternal::DisconnectCallback,
qtjambi_disconnect_callback);
QInternal::registerCallback(QInternal::AdoptCurrentThread,
qtjambi_adopt_current_thread);
#if QT_VERSION >= 0x040300
QInternal::registerCallback(QInternal::EventNotifyCallback,
qtjambi_event_notify);
#endif
}
...
static bool qtjambi_event_notify(void **data)
{
QEvent *event = (QEvent *) data[1];
bool *result = (bool *) data[2];
switch (event->type()) {
case 512:
qtjambi_metacall(qtjambi_current_environment(), event);
*result = true;
return true;
case QEvent::ChildAdded:
case QEvent::ChildRemoved:
{
QChildEvent *e = static_cast<QChildEvent *>(event);
QObject *parent = reinterpret_cast<QObject *>(data[0]);
// We're not interested in objects that don't have a link as the
GC won't
// be interferring with them anyway.
QtJambiLink *link = QtJambiLink::findLinkForQObject(e->child());
if (link) {
if (link->qobject()) {
if (e->added())
link->setCppOwnership(qtjambi_current_environment(),
link->javaObject(qtjambi_current_environment()));
else
link->setDefa
...
I didn't know about QChildEvents before seeing this, but it looks like a
really useful way to tell if we need to keep a strong reference of a child to
prevent the mono runtime from garbage collecting it. I don't think we need to
add a callback like the above code because we can just trap the virtual
method callback for QObject::childEvent() in the same place we do for
qt_metacall(). If the childEvent is a ChildAdded we add a strong ref to the
map, or if it's a ChildRemoved we remove it.
-- Richard
More information about the Kde-bindings
mailing list