[newbie] signal/slot with sublassing problem
Hi everyone, this is probably a newbie question, but I couldn't readily find an answer so would like to ask the group for help.
I have a class hierarchy like the following:
Code:
namespace A {
class GenericService {
public :
void sendQuery();
protected slot:
}
}
void GenericService::sendQuery()
{
/* do a bunch of stuff that results in a query being sent through RemoteService */
}
namespace B {
class SomeServiceType : public GenericService {
public :
/* some specialized functions */
}
class MoreSpecificServiceType : public SomeServiceType {
public :
/* more specialized functions */
}
}
My problem is that when I execute a query from an instance of MoreSpecificServiceType the connect fails to find the slot B::SomeServiceType::receiveData unless I specifically create that function, which then hides the default behavior of A::GenericService::receiveData which is what I want to have actually handle this signal. I'd have thought that the specialized classes would inherit the appropriate slot from the original base class and I'd not need to reimplement it. Can anyone let me know what the correct approach here is? or have I just made a general mistake someone can point out?
many thanks for your help
Nathan
Re: [newbie] signal/slot with sublassing problem
The issue is probably that you need to explicitly qualify the namespace in the SIGNAL/SLOT signatures in your connect call.
Read http://lists.trolltech.com/qt-intere...ad01111-0.html
If that does not help, please post the exact error messages you are getting.
Re: [newbie] signal/slot with sublassing problem
Thanks for the reply and the link. I'll have a more thorough go through tonight when I get home from work and also post the exact code and message and maybe build a simple test case app if the problem is still happening.
My first thought though is that how can I supply a namespace in a base class that won't know what namespace any future subclasses are in. In my example above, how could the connect in the base class in namespace A know what namespace the derived class in namespace B has?
thanks
N
Re: [newbie] signal/slot with sublassing problem
hmmm.... I'm making things worse it seems :confused:
I have 4 files for a test app:
sender.h:
Code:
#include <QApplication>
public :
void pokeMe() {emit sendData();}
signals :
void sendData() {}
};
genericservice.h
Code:
#ifndef GS_H
#define GS_H
#include <QApplication>
#include <QDebug>
#include "sender.h"
namespace A {
class GenericService
: public QObject { public :
void sendQuery();
protected slots:
void receiveData();
private :
Sender *m_sender;
};
}
#endif
genericservice.cpp
Code:
#include "genericservice.h"
using namespace A;
{
m_sender = new Sender(this);
}
void GenericService::sendQuery()
{
connect(m_sender, SIGNAL(sendData()), this, SLOT(receiveData()));
m_sender->pokeMe();
}
void GenericService::receiveData()
{
}
and main.cpp
Code:
#include <QApplication>
#include "genericservice.cpp"
int main(int argc, char *argv[])
{
GenericService *gen = new GenericService();
gen->sendQuery();
return app.exec();
}
and now when I build and execute I get:
Object::connect: No such signal QObject::sendData() in genericservice.cpp:13
i.e. the signal from a non-namespaced class is not not being recognized in the namespaced one. I didn't even get around to the derived classes in different namespaces! Could really use some help on this!
Re: [newbie] signal/slot with sublassing problem
you forgot the Q_OBJECT macro. It should look like this:
Code:
Q_OBJECT
public :
/* . . . */
};
without this macro the MOC (Meta Object Compiler) don't make any interest to your class and that's bad, bacause the meta object system is the thing making signal/slots working.
Re: [newbie] signal/slot with sublassing problem
ok, the files above don't work because I'm an idiot ^W^W^W I forgot the Q_OBJECT macro...
Re: [newbie] signal/slot with sublassing problem
Quote:
Originally Posted by
faldżip
you forgot the Q_OBJECT macro. It should look like this:
Code:
Q_OBJECT
public :
/* . . . */
};
without this macro the MOC (Meta Object Compiler) don't make any interest to your class and that's bad, bacause the meta object system is the thing making signal/slots working.
wow, you posted just before me! just made the change and of course everything went much better... now checking my real code for the same mistake.
cheers
N
Re: [newbie] signal/slot with sublassing problem
Yup... it was a forgotten Q_OBJECT macro. Thanks for the help, I guess I can chalk this one up to experience.
thanks
Nathan