diff --git a/src/blackmisc/slot.h b/src/blackmisc/slot.h index b7aa0967d..780b859bd 100644 --- a/src/blackmisc/slot.h +++ b/src/blackmisc/slot.h @@ -78,6 +78,13 @@ namespace BlackMisc CSlot(T *object, R(U::* function)(Args...)) : m_object(object), m_function([ = ](Args... args) { return (object->*function)(args...); }) + { + Q_ASSERT_X(object, Q_FUNC_INFO, "Need object"); + } + + //! Construct a slot from the given object passing a function + CSlot(std::function function) : + m_function(function) {} //! Call the slot. The behaviour is undefined if the slot is empty. @@ -94,6 +101,14 @@ namespace BlackMisc return m_object.data(); } + //! Set 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. + void setObject(QObject *object) + { + Q_ASSERT_X(hasNullObject(), Q_FUNC_INFO, "Can only set, not change the object"); + m_object = object; + } + //! True if the slot can be called, false if it is empty. operator bool() const { diff --git a/tests/blackmisc/testslot.cpp b/tests/blackmisc/testslot.cpp index 320beab50..d4b6dca12 100644 --- a/tests/blackmisc/testslot.cpp +++ b/tests/blackmisc/testslot.cpp @@ -30,8 +30,9 @@ namespace BlackMiscTest CSlot slot1 = { obj, &QObject::setObjectName }; QVERIFY2(slot1, "Slot has valid object and function - can be called."); - CSlot slot2 = { static_cast(nullptr), &QObject::setObjectName }; - QVERIFY2(!slot2, "Slot has an invalid pointer - cannot be called."); + // KB 8/17 T125, CSlot can no longer be constructed with null object + // CSlot slot2 = { static_cast(nullptr), &QObject::setObjectName }; + // QVERIFY2(!slot2, "Slot has an invalid pointer - cannot be called."); CSlot slot3; QVERIFY2(!slot3, "Slot has an invalid pointer and invalid function - cannot be called.");