PDA

View Full Version : Problem with Signal/Slot connections in derived classes



hb
20th September 2007, 10:50
I have a problem with Signal/Slot connections when a slot exists in a derived class, but not the baseclass. Imagine the following code, where class A is derived from QObject, and class B is derived from class A.

Class A:


#include <QObject>

class A : public QObject
{
Q_OBJECT

public:
A(QObject *parent = NULL);
virtual ~A() {};
};

A::A(QObject *parent) : QObject(parent)
{
}


Class B:


#include "A.h"

class B : public A
{
public:
B(QObject *parent = NULL);
virtual ~B() {};

public slots:
void mySlot();
};

B::B(QObject *parent) : A(parent)
{
}

void B::mySlot()
{
qDebug("B::mySlot called");
}


Now I want to create an instance of class B, and connect the slot defined in class B (but not in class A!) to a signal.



QApplication app(argc, argv);
B b;
QObject::connect(&app, SIGNAL(aboutToQuit()), &b, SLOT(mySlot()));


Instead of the slot being called, I get the following warning in the debug output:


WARNING: Object::connect: No such slot A::mySlot()
WARNING: Object::connect: (sender name: 'tst_Qt')


Why does it try to connect to A::mySlot()? b is clearly an instance of B, not of A.
Qt is version 4.3.1. I'd be very happy about any hint to solve this problem..

marcel
20th September 2007, 11:32
Add the Q_OBJECT macro to B also.

hb
20th September 2007, 12:27
Ouch. That was really a stupid mistake. Thanks for your help!