[Development] Module maintainers: QT_NO_CONTEXTLESS_CONNECT in your modules
Volker Hilsheimer
volker.hilsheimer at qt.io
Wed Jul 12 11:49:50 CEST 2023
> Am 10.07.2023 um 18:02 schrieb Giuseppe D'Angelo via Development:
>> 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().
>>
> On 11 Jul 2023, at 11:56, Hasselmann Mathias via Development <development at qt-project.org> wrote:
>
> Hi Peppe,
>
> thank you alot for this highly appreciated and super useful feature!
> Give up counting how often I forgot the context argument.
>
Thanks Peppe for taking the initiative!
The change has now merged for qtbase, and given the war stories shared here it would indeed be great if we can all clean up the remaining modules so that we can enable this as widely as possible.
Volker
More information about the Development
mailing list