diff --git a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h index 47eca9eed..13c4859bf 100644 --- a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h +++ b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h @@ -99,7 +99,18 @@ } else { \ return env.Null(); \ } \ - } + } \ + Napi::Value deleteLater(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + this->instance->deleteLater(); \ + return env.Null(); \ + } \ + Napi::Value deleteObject(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + delete static_cast(this->instance); \ + return env.Null(); \ + } \ + // Ideally this macro below should go in // QOBJECT_WRAPPED_METHODS_DECLARATION_WITH_EVENT_SOURCE but some wrappers @@ -143,7 +154,9 @@ InstanceMethod("setParent", &ComponentWrapName::setParent), \ InstanceMethod("startTimer", &ComponentWrapName::startTimer), \ InstanceMethod("killTimer", &ComponentWrapName::killTimer), \ - InstanceMethod("parent", &ComponentWrapName::parent), + InstanceMethod("parent", &ComponentWrapName::parent), \ + InstanceMethod("deleteLater", &ComponentWrapName::deleteLater), \ + InstanceMethod("delete", &ComponentWrapName::deleteObject), #endif // QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h b/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h index d7d1fb8fd..7c8037c53 100644 --- a/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h +++ b/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h @@ -147,8 +147,8 @@ class DLL_EXPORT WrapperCache : public QObject { static Napi::FunctionReference destroyedCallback; public Q_SLOTS: - void handleDestroyed(const QObject* object) { - uint64_t ptrHash = extrautils::hashPointerTo53bit(object); + void handleDestroyed(const QObject* qobject) { + uint64_t ptrHash = extrautils::hashPointerTo53bit(qobject); if (!this->cache.contains(ptrHash)) { return; } diff --git a/src/lib/QtCore/QObject.ts b/src/lib/QtCore/QObject.ts index 65d86f4b9..53ae0feff 100644 --- a/src/lib/QtCore/QObject.ts +++ b/src/lib/QtCore/QObject.ts @@ -64,6 +64,12 @@ export class QObject extends Ev killTimer(timerId: number): void { this.native.killTimer(timerId); } + delete(): void { + this.native.delete(); + } + deleteLater(): void { + this.native.deleteLater(); + } } export interface QObjectSignals { diff --git a/src/lib/core/__test__/WrapperCache.test.ts b/src/lib/core/__test__/WrapperCache.test.ts index 678655c3f..4f614b762 100644 --- a/src/lib/core/__test__/WrapperCache.test.ts +++ b/src/lib/core/__test__/WrapperCache.test.ts @@ -59,5 +59,19 @@ describe('WrapperCache using CacheTestQObject', () => { expect((b.parent())['magic']).toBe(true); }); + it('QObject.delete() clears the native field', () => { + const a = new QObject(); + a.delete(); + expect(a.native).toBeNull(); + }); + + it('QObject.delete() clears chains of QObjects and their native field', () => { + const a = new QObject(); + const b = new QObject(a); + a.delete(); + expect(a.native).toBeNull(); + expect(b.native).toBeNull(); + }); + qApp.quit(); });