Search code examples
c++qtqt-signalsslot

Qt Signal Slots class scope types


Given two headers from an external SDK,

class A
{
  public:
   class B
   {
   };
  public slots:
   void onValue(B* b);
};

class C
{
  signal:
   void value(A::B* b);
};

The question is how i can connect signal and slots of C -> A since the data types are taken as incompatible in run time.


Solution

  • As you noticed, code below report a runtime error reporting incompatibility between signal and slot:

    QObject::connect( &c, SIGNAL( value( A::B* ) ), &a, SLOT( onValue( B* ) ) );
    

    Error is:

    QObject::connect: Incompatible sender/receiver arguments C::value(A::B*) --> A::onValue(B*)

    This is because SIGNAL and SLOT are macros and the compatibility of calls is resolved at runtime and fails this case when it should work as A::B and B are the same (most likely because it's apparently a simple text comparison).

    But this is an old style connection command (pre Qt5).

    With the new style connection command:

    QObject::connect( &c, &C::value, &a, &A::onValue );
    

    No error is reported at compilation time, nor at runtime and the connection will work.