From 0fa6a6e4c36c6d35c498963d82208b73aea76ad3 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 17 Apr 2020 19:02:37 +0200 Subject: [PATCH] Ref T786, singleShot for CSlot --- src/blackmisc/slot.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/blackmisc/slot.h b/src/blackmisc/slot.h index 67e8a8d60..d83b4afed 100644 --- a/src/blackmisc/slot.h +++ b/src/blackmisc/slot.h @@ -11,14 +11,17 @@ #ifndef BLACKMISC_SLOT_H #define BLACKMISC_SLOT_H +#include "blackmisc/invoke.h" + #include #include #include +#include + #include #include #include #include -#include "blackmisc/invoke.h" namespace BlackMisc { @@ -100,6 +103,19 @@ namespace BlackMisc return m_function(args...); } + //! Call function "de-coupled" in original thread + bool singleShot(Args... args) const + { + // does NOT return the values of m_function! + if (!m_object || !m_function) { return false; } + QTimer::singleShot(0, m_object.data(), [ = ] + { + if (!m_object || !m_function) { return; } + m_function(args...); + }); + return true; + } + //! Returns the object which the slot belongs to. //! Use this as the third argument to QObject::connect to ensure the slot is called in the correct thread. QObject *object() const