add async call on main

This commit is contained in:
王劲鹏
2021-02-09 10:38:27 +08:00
committed by osborn
parent 60218cd8dd
commit b406460a8d
7 changed files with 29 additions and 3 deletions

View File

@@ -13,6 +13,21 @@ public:
QFuture<std::function<void()>::result_type> future =
QtConcurrent::run(threadPool, lambda);
}
template <typename Function>
static void ensureRunInMain(Function &&function) {
struct Event : public QEvent {
using DecayedFunction = typename std::decay<Function>::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>(function)));
}
};
#endif // ASYNC_CALL_H