From 120e45f583a2526feb2296cf3475c6b95b46b61c Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 8 Jun 2019 14:26:01 +0200 Subject: [PATCH] cleanest implementation for now. No dependency on main widget class --- src/cpp/QtWidgets/QPushButton/npushbutton.h | 22 ------------------- .../QPushButton/qpushbutton_wrap.cpp | 1 + .../QtWidgets/QPushButton/qpushbutton_wrap.h | 13 ++++++++--- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/cpp/QtWidgets/QPushButton/npushbutton.h b/src/cpp/QtWidgets/QPushButton/npushbutton.h index 16e7ab12b..21af214c3 100644 --- a/src/cpp/QtWidgets/QPushButton/npushbutton.h +++ b/src/cpp/QtWidgets/QPushButton/npushbutton.h @@ -4,35 +4,13 @@ #include #include "src/cpp/core/YogaWidget/yogawidget.h" #include "napi.h" -#include "napi-thread-safe-callback.hpp" -#include "deps/spdlog/spdlog.h" class NPushButton: public QPushButton, public YogaWidget { -private: - std::unique_ptr emitRef; public: SET_YOGA_WIDGET_Q_PROPERTIES using QPushButton::QPushButton; //inherit all constructors of QPushButton - NPushButton(){ - connect(this, &QPushButton::clicked, - [=](bool val) { - QString eventType = "clicked"; - emitRef->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, eventType.toStdString()), Napi::Boolean::New(env, val) }; - }); - } - ); - } - ~NPushButton() { - this->emitRef.reset(); - }; Q_OBJECT - -public: - void setNodeEmitterEmit( std::unique_ptr &emitterEmit){ - this->emitRef = std::move(emitterEmit); - } }; diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index 132369f05..d738bae35 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -37,6 +37,7 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr } QPushButtonWrap::~QPushButtonWrap() { + this->emitterEmit.release(); delete this->instance; } diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h index b0520e2fc..3aace90cc 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -8,6 +8,7 @@ class QPushButtonWrap : public Napi::ObjectWrap { private: NPushButton* instance; + std::unique_ptr emitterEmit; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QPushButtonWrap(const Napi::CallbackInfo& info); @@ -20,9 +21,15 @@ class QPushButtonWrap : public Napi::ObjectWrap { Napi::Value setNodeEventEmiiter(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - std::unique_ptr emitterEmit = std::make_unique(info[0].As()); - this->instance->setNodeEmitterEmit(emitterEmit); - emitterEmit.release(); + this->emitterEmit = std::make_unique(info[0].As()); + + NPushButton::connect(this->instance, &QPushButton::clicked, + [=](bool val) { + this->emitterEmit->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "clicked"), Napi::Boolean::New(env, val) }; + }); + } + ); return env.Null(); }