[Development] Module maintainers: QT_NO_CONTEXTLESS_CONNECT in your modules

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Mon Jul 10 18:02:55 CEST 2023


Hi,

https://codereview.qt-project.org/c/qt/qtbase/+/487560 introduces 
QT_NO_CONTEXTLESS_CONNECT , a macro that disables the 3-arguments 
connect -- in other words, it disables the

>   QObject::connect(sender, signal, functor)

overload, leaving only 4/5 argument(s) overloads

> QObject::connect(sender, signal, receiver/context, functor/slot, (type))


as well as the string-based connect().


The reason for NOT using the 3-args overload is that it is error prone.

For starters, it makes it hard to reason about the lifetime of such a 
connection. It makes it very easy to connect to lambdas that capture 
some local state in the receiver, but when the receiver is destroyed, 
the connection isn't automatically disconnected (therefore, if the 
signal is emitted, the program will crash). Fixing this may or may not 
be straightforward, depending on how much state is captured.

Second, it's also easy to forget that since there's no receiver/context, 
the connection is always forced to be *direct*, which complicates things 
if multiple threads are involved.

--

I'm about to enable the macro for (most) of QtBase:

https://codereview.qt-project.org/c/qt/qtbase/+/489232

I've also done the exercise of enabling it for qtdeclarative (patches 
pending) and qttools (merged), but there's simply too much code out 
there that *still* uses it. As I said above, fixes are not always 
obvious (sometimes it's very unclear who the "receiver" is).

--

Since this is ultimately a code style issue, I'd like to leave the 
decision with the module maintainers regarding whether or not enable 
QT_NO_CONTEXTLESS_CONNECT for their own modules. Ideally, however, we 
should enable it in headersclean, so please make at least those not 
contain 3-args connect().


Opinions? Objections?


Thank you,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4244 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/development/attachments/20230710/68a6804f/attachment.bin>


More information about the Development mailing list