[Qt-interest] Qt, C++ Namespaces, and Signals/Slots
Constantin Makshin
cmakshin at gmail.com
Thu Jun 24 18:28:52 CEST 2010
Parameters of SIGNAL() and SLOT() macros must be exactly equal to corresponding signal/slot declaration. So in your example the connect() statement should look like this:
connect(_a, SIGNAL(someSignal(X:Y:anEnum)), this, SLOT(someSlot(X:Y:anEnum));
And remember that Qt's meta object system doesn't support nested types. For example:
class ClassA
{
public:
class SubClassA
{
...
};
...
signals:
void someSignal (const ClassA::SubClassA&); // <- !!!
};
class ClassB
{
...
public slots:
void someSlot (const ClassA::SubClassA&);
};
Note that you have to write complete parameter type specifier in someSignal() because if you write it as "void someSignal(const SubClassA&);", it'll be correct from C++ point of view, but any attempt to connect someSignal() and someSlot() will fail with "parameter type mismatch" type of error. I had this problem during development of one of my applications and would like to help you to avoid it. :)
On Thursday 24 June 2010 19:19:21 BRM wrote:
> I am moving around some definitions from a Qt-only header set to a shared Qt and Standard C++ header sets.
> In the new header sets I am utilizing C++ namespaces. Updating everything seems to have gone fine, but thus far I've only tested using MSVC.
> Presently this just applies for a series of enums, and a few structures as well; a number of these get used in signals and slots.
> Not every use of the types warrants the implementation file having a using namespace for the namespace these are stored in - and I will have multiple levels of namespaces.
>
> What I am curious about is that in one case the function definitions for the signals/slots included the namespaces;
> however, I didn't put the namespaces in the connect() statement - or the SIGNAL()/SLOT() sub-parts of it; yet, Qt seems to have made the right connections, and allowed it.
> I expected I would have to update the SIGNAL()/SLOT() to have the namespace parts in it too. Presently I have only noticed this for some standard C++ enums that I reference.
> But I am wondering if this is by design or some fluke - e.g. can I rely on this, or should I really namespace the parameters?
>
> Where I have used qRegisterMetaType<>() to register something I did add in the namespace referencing, but I have not done that in all cases.
>
> Example:
>
> namespace X {
> namespace Y {
> enum anEnum {
> anEnumValue1=0,
> anEnumValue2,
> ...
> };
> }
> }
>
> class myQtClassA : QObject {
> ...
> Q_SIGNALS:
> void someSignal(X:Y:anEnum...);
> ...
> };
>
> class myQtClassB: QObject {
> ...
> public Q_SLOTS:
> void someSlot(X:Y:anEnum);
> ...
> };
>
> myQtClassB::someFunction(myQtClassA* _a){
> ...
> connect(_a,SIGNAL(someSignal(anEnum)),this,SLOT(someSlot(anEnum));
> ...
> }
>
> TIA,
>
> Ben
More information about the Qt-interest-old
mailing list