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>
{
Q_OBJECT
public:
virtual ~A() {};
};
{
}
#include <QObject>
class A : public QObject
{
Q_OBJECT
public:
A(QObject *parent = NULL);
virtual ~A() {};
};
A::A(QObject *parent) : QObject(parent)
{
}
To copy to clipboard, switch view to plain text mode
Class B:
#include "A.h"
class B : public A
{
public:
virtual ~B() {};
public slots:
void mySlot();
};
{
}
void B::mySlot()
{
qDebug("B::mySlot called");
}
#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");
}
To copy to clipboard, switch view to plain text mode
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.
B b;
QObject::connect(&app,
SIGNAL(aboutToQuit
()),
&b,
SLOT(mySlot
()));
QApplication app(argc, argv);
B b;
QObject::connect(&app, SIGNAL(aboutToQuit()), &b, SLOT(mySlot()));
To copy to clipboard, switch view to plain text mode
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')
WARNING: Object::connect: No such slot A::mySlot()
WARNING: Object::connect: (sender name: 'tst_Qt')
To copy to clipboard, switch view to plain text mode
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..
Bookmarks