Add QObject.children()

This commit is contained in:
Simon Edwards 2022-05-05 19:25:01 +02:00
parent 880ea7c998
commit f3d7d73acc
3 changed files with 26 additions and 1 deletions

View File

@ -109,6 +109,16 @@
Napi::Env env = info.Env(); \
delete static_cast<QObject*>(this->instance); \
return env.Null(); \
} \
Napi::Value children(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
QObjectList children = this->instance->children(); \
Napi::Array resultArrayNapi = Napi::Array::New(env, children.size()); \
for (int i = 0; i < children.size(); i++) { \
resultArrayNapi[i] = \
WrapperCache::instance.getWrapper(env, children[i]); \
} \
return resultArrayNapi; \
}
// Ideally this macro below should go in
@ -155,7 +165,8 @@
InstanceMethod("killTimer", &ComponentWrapName::killTimer), \
InstanceMethod("parent", &ComponentWrapName::parent), \
InstanceMethod("deleteLater", &ComponentWrapName::deleteLater), \
InstanceMethod("delete", &ComponentWrapName::deleteObject),
InstanceMethod("delete", &ComponentWrapName::deleteObject), \
InstanceMethod("children", &ComponentWrapName::children),
#endif // QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE

View File

@ -70,6 +70,9 @@ export class QObject<Signals extends QObjectSignals = QObjectSignals> extends Ev
deleteLater(): void {
this.native.deleteLater();
}
children(): QObject[] {
return this.native.children().map((kid: any) => wrapperCache.getWrapper(kid));
}
}
export interface QObjectSignals {

View File

@ -82,5 +82,16 @@ describe('WrapperCache using CacheTestQObject', () => {
expect(b.native).toBeNull();
});
it('Object.children()', () => {
wrapperCache._flush();
const parent = new QObject();
const kid1 = new QObject(parent);
const kid2 = new QObject(parent);
const allKids = parent.children();
expect(allKids.length).toBe(2);
expect(allKids[0]).toEqual(kid1);
expect(allKids[1]).toEqual(kid2);
});
qApp.quit();
});