Ref T786, singleShot for CSlot

This commit is contained in:
Klaus Basan
2020-04-17 19:02:37 +02:00
committed by Mat Sutcliffe
parent 388138203e
commit 0fa6a6e4c3

View File

@@ -11,14 +11,17 @@
#ifndef BLACKMISC_SLOT_H #ifndef BLACKMISC_SLOT_H
#define BLACKMISC_SLOT_H #define BLACKMISC_SLOT_H
#include "blackmisc/invoke.h"
#include <QPointer> #include <QPointer>
#include <QObject> #include <QObject>
#include <QtGlobal> #include <QtGlobal>
#include <QTimer>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <future> #include <future>
#include <memory> #include <memory>
#include "blackmisc/invoke.h"
namespace BlackMisc namespace BlackMisc
{ {
@@ -100,6 +103,19 @@ namespace BlackMisc
return m_function(args...); 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. //! 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. //! Use this as the third argument to QObject::connect to ensure the slot is called in the correct thread.
QObject *object() const QObject *object() const