Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable old (Qt4-style) Signal-Slot syntax in Qt5 code

Is there any way to prevent or discourage the use of the old Signal-Slot syntax from Qt4 in projects solely written in Qt5?

In our current project, there are no occurrences of the old syntax and I don't see any reason to support them either. Thus we want to disable it completely to prevent accidental use. Is this possible, e.g. by defining certain symbols in the .pro files?

I know this should be possible with custom Linter rules but we don't have that centralized yet unfortunately.

//old way. should throw a compiler error or warning
connect(sender, SIGNAL(sig), receiver, SLOT(slt));

//new way
connect(sender, &Send::sig, receiver, &Rec::slt);
like image 464
perivesta Avatar asked Sep 05 '19 10:09

perivesta


Video Answer


1 Answers

If you have a shared header file in the project that you can ensure will be included after QObject, you can do this:

#define SIGNAL(x) static_assert(false, "String-based signal/slot syntax has been disabled in this project")

Same for SLOT.

If you want to turn it into warning, check out this answer. I definitely agree with you that the string based syntax is a plague and shouldn't occur outside of uiced files.

like image 109
Tomáš Zato - Reinstate Monica Avatar answered Sep 22 '22 02:09

Tomáš Zato - Reinstate Monica