Unsolved Connect to signal emitted from reference.
-
Is there a reason for not providing a connect method overload to connect a signal from reference?
For example;MyClass::MyClass(OtherClass& ref): m_refmember(ref) { connect (m_refmember, &OtherClass::someSignal, this, &MyClass::someSlot); }
Assume that I don't know the internals of the connect mechanism.
-
@hsolter said in Connect to signal emitted from reference.:
Is there a reason for not providing a connect method overload to connect a signal from reference?
Mainly because Qt encourages the convention of allocating QObjects using
new
(except for top-level, parentless objects). You can allocate all your QObjects on the stack if you want, but be careful of construction/destruction order to avoid crashes: http://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjectsAlso, adding reference-based overloads would almost quadruple (4x) the total number of overloads:
- Pointer sender, pointer receiver
- Pointer sender, reference receiver
- Reference sender, pointer receiver
- Reference sender, reference receiver
-
@JKSH said in Connect to signal emitted from reference.:
Mainly because Qt encourages the convention of allocating QObjects using new (except for top-level, parentless objects).
I don't believe it "encourages" this, only that it's the conventional way to do it since
QObject
can't be copied. In fact allocating in the stack will always outperform the additional call to the heap manager and makes memory leaks absolutely impossible. You do need, however, as you noted, to ensure the objects are in a stack - i.e. first-in-last-out. -
@hsolter said in Connect to signal emitted from reference.:
Is there a reason for not providing a connect method overload to connect a signal from reference?
is there any reason to do so?
I mean you just need to add a single character to your code:connect (&m_refmember, &OtherClass::someSignal, this, &MyClass::someSlot);