[Development] Enum classes in signals?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Wed Feb 6 05:41:39 CET 2019


Il 05/02/19 18:16, Dmitriy Purgin ha scritto:
> I couldn't figure out the exact combination but as far as I remember, if 
> you have namespaced code, you have to always fully qualify the enum 
> class parameters in signals and slots.

This is actually also the case for enums and enum classes. For instance, 
consider

class A : public QObject {
   Q_OBJECT
public:
   enum class E { E1, E2, E3 };
signals:
   void mySignal(E);
};

class B : public QObject {
   Q_OBJECT
public slots:
   void mySlot(A::E);
};

This code compiles just fine, but you will NOT be able to connect 
mySignal1 to mySlot using SIGNAL/SLOT.

Doing it like this

   connect(a, SIGNAL(mySignal(E)), b, SLOT(mySlot(A::E)));

won't work because the argument lists don't match (this connect version 
compares the parameter lists _as strings_, and obviously "E" is 
different from "A::E").


Connecting it like this

   connect(a, SIGNAL(mySignal(A::E)), b, SLOT(mySlot(A::E)));

will not find mySignal in A, because the lookup will search for a 
function with that signature _spelled exactly that way in the source 
code_. Such a function is not there; the source code spells "E" as 
parameter type, not "A::E".

The solution hence is to declare the signal with an A::E parameter.

My 2 c,
-- 
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: 4007 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20190206/9f5cffe4/attachment.bin>


More information about the Development mailing list