0

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.

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
  • B is private in the scope of A, so you can't use it outside. You could do the unsafe thing and use a void pointer, then cast it to B pointer in the slot. But seeing that B is private, there is no possible way to emit from anywhere other than inside A. – dtech Feb 08 '17 at 16:47
  • @ddriver sorry it was a typo: B is public. – code_not_yet_complete Feb 08 '17 at 16:49
  • What's the error message? Seems to me that the types are actually compatible. – goug Feb 08 '17 at 18:06
  • Theoretically they are. But qt only cares about the name in the signal and slot function when connecting and complains on runtime that connect is not compatible. – code_not_yet_complete Feb 08 '17 at 18:14

1 Answers1

2

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.

jpo38
  • 20,821
  • 10
  • 70
  • 151