0

I have a signal that is emitted w/a parameter taken from a subclass, but in one slot implementation, I don't need a special handler for the subclass; just the parent class. I thought that connect() would upcast that parameter, but unfortunately, I get:

QObject::connect: Incompatible sender/receiver arguments
        emitter::mysignal(messageSubclass *) --> slotter::generalHandler(messageSuperclass *)

In emitter I have:

emit mysignal((messageSubclass *)msg_ptr );

There are various places in the code were I need to handle this messageSubclass specifically. But, in one place, a whole family of signals can be handled by just one slot method, so I have:

connect(emitter_ptr, SIGNAL(mysignal(messageSubclass *)), this, SLOT(generalHandler(messageSuperclass *)));

I really don't want to have to have generalHandler() be replaced by a couple of dozen specific handlers, that all do the same thing. Putting it another way, I'm trying to avoid something tedious like:

connect(emitter_ptr, SIGNAL(mysignal(messageSubclass *)), this, SLOT(specificHandler(messageSubclass *)));
connect(emitter_ptr, SIGNAL(mysignalA(messageSubclassA *)), this, SLOT(specificHandlerA(messageSubclassA *)));
connect(emitter_ptr, SIGNAL(mysignalB(messageSubclassB *)), this, SLOT(specificHandlerB(messageSubclassB *)));
connect(emitter_ptr, SIGNAL(mysignalC(messageSubclassC *)), this, SLOT(specificHandlerC(messageSubclassC *)));
...

void handlerClass::specificHandler(messageSubclass *msg_ptr) {
    generalHandler((messageSuperclass *)msg_ptr);
}

void handlerClass::specificHandlerA(messageSubclassA *msg_ptr) {
    generalHandler((messageSuperclass *)msg_ptr);
}

void handlerClass::specificHandlerB(messageSubclassB *msg_ptr) {
    generalHandler((messageSuperclass *)msg_ptr);
}

void handlerClass::specificHandlerC(messageSubclassC *msg_ptr) {
    generalHandler((messageSuperclass *)msg_ptr);
}

And again, I need to have

signals:
    mysignal(messageSubclass *);

for other slots which need messageSubclass.

I'm using Qt 4.8

Opux
  • 702
  • 1
  • 10
  • 30
  • Which is the base class, and which is the derived class? "Sub" and "super" aren't clear to me. If "sub" is base, and "super" is derived, then the compiler is correct. If it's the other way around, then in the code where the connect statement is, have you included the class definition for the derived class so that the compiler knows that it can cast? – goug Mar 06 '17 at 19:27
  • Other way round: class messageSubclass : public messageSuperclass; It sounds like you are saying I'm missing an #include, which is strange, because there's already an #include "messageSubclass.h" which will include the superclass. – Opux Mar 06 '17 at 19:32
  • It was a question, not a statement, but it sounds like you have that covered. Is the error run-time, or compilation? – goug Mar 06 '17 at 22:16
  • @goug Run-time. So you are saying that `connect()` *should* be able to upcast the parameters? – Opux Mar 06 '17 at 22:23

0 Answers0