[Qt-interest] Connect signal to slot with derived parameter

John McClurkin jwm at nei.nih.gov
Fri Oct 29 14:01:00 CEST 2010


Andras Toth wrote:
> Hi,
> 
> Is there a way to connect a signal with a single parameter, which is a pointer 
> to a derived class to a slot with a single parameter, which is a pointer to a 
> base class?
> 
> If I run the code below, I get the following message:
> 
> QObject::connect: Incompatible sender/receiver arguments
>         SomeObject::sig1(Derived*) --> SomeObject::slot1(Base*)
> 
> Qt Documentation just says "The signature of a signal must match the signature 
> of the receiving slot." In my opinion, these signatures  should be matching, 
> since directly  calling SomeObject::slot1() with a pointer to an instance of 
> Derived is permitted.
> 
> Thanks,
> 
> Andras
> 
> PS: using Qt. 4.7, and 4.6.2 (for Maemo) in case it matters.
> 
> 
> 
> 
> // main.cpp
> 
> #include <QDebug>
> #include <QtGui/QApplication>
> 
> 
> class Base {};
> 
> class Derived : public Base {};
> 
> class SomeObject : public QObject
> {
>     Q_OBJECT
> public:
>     SomeObject();
>     void foo();
> 
> signals:
>     void sig1(Derived * d);
> 
> public slots:
>     void slot1(Base * b);
> };
> 
> 
> SomeObject::SomeObject()
> {
>     connect(this, SIGNAL(sig1(Derived*)),
>             this, SLOT(slot1(Base*)));
> }
> 
> void SomeObject::foo()
> {
>     emit sig1(0);
> }
> 
> void SomeObject::slot1(Base * b)
> {
>     Q_UNUSED(b);
>     qDebug() << "slot1";
> }
> 
> int main(int argc, char *argv[])
> {
>     SomeObject so;
>     so.foo();
>     return 0;
> }
> 
> #include "main.moc"

You should be able to cast the pointer Derived * to Base *. Something 
like SIGNAL(sig1((Base *)Derived *)...



More information about the Qt-interest-old mailing list