From 66649fe0f0ed2fcf31a107d1dc61d303061765be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Mon, 22 Feb 2021 18:54:29 +0800 Subject: [PATCH] new way to impl main thread --- doric-Qt/doric/async/DoricAsyncCall.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/doric-Qt/doric/async/DoricAsyncCall.h b/doric-Qt/doric/async/DoricAsyncCall.h index 7f13e882..cfa62dca 100644 --- a/doric-Qt/doric/async/DoricAsyncCall.h +++ b/doric-Qt/doric/async/DoricAsyncCall.h @@ -15,18 +15,11 @@ public: } template - static void ensureRunInMain(Function &&function) { - struct Event : public QEvent { - using DecayedFunction = typename std::decay::type; - DecayedFunction decayedFunction; - Event(DecayedFunction &&decayedFunction) - : QEvent(QEvent::None), decayedFunction(std::move(decayedFunction)) {} - Event(const DecayedFunction &decayedFunction) - : QEvent(QEvent::None), decayedFunction(decayedFunction) {} - ~Event() { decayedFunction(); } - }; - QCoreApplication::postEvent(qApp, - new Event(std::forward(function))); + static void ensureRunInMain(Function &&function, + QThread *thread = qApp->thread()) { + auto *obj = QAbstractEventDispatcher::instance(thread); + Q_ASSERT(obj); + QMetaObject::invokeMethod(obj, std::forward(function)); } };