PDA

View Full Version : connecting signals and slots from different namespaces



mattc
17th July 2009, 19:02
Hello,

I have just solved a weird problem connecting signals and slots from different namespaces, but I want to be sure I am not complicating matters. I have the following class:


namespace MyLibrary
{
class Data {

};

class Emitter {
signals:
void newData(const Data& d);
};

}

Then, in MyApplication, I'm trying to connect to the "newData" signal as follows:


namespace MyApplication {
...
connect(emitter, SIGNAL(newData(const MyLibrary::Data&)), this, SLOT(onNewData(const MyLibrary::Data&)));
...
}

but this does not work. To make it work, I needed to change the Emitter class as follows:


class Emitter {
signals:
void newData(const MyLibrary::Data& d); // added namespace specifier: completely useless as far as C++ is concerned, I believe
};

Is this the standard way to fix the problem? Am I missing something?

Thank you

wysota
17th July 2009, 20:16
Signal and slot names are compared character by character thus tricks like the one you used are required. Of course you can get away from this with the "using" keyword but your method is also fine.

PaceyIV
18th July 2009, 14:18
I think my problem is similar.

I've got a class and inside a struct definition. The class also emit a signal with this structure.



class MatrixModule : public QObject
{
Q_OBJECT

public:
struct DataReceived
{
int packetID; //!< Packet ID
int linkQualityIndicator; //!< Link Quality Indicator gives feedback to the strength of the received packet

// Host/Application

Address source; //!< Source Transceiver Address
Address destination; //!< Destination Transceiver Address

QByteArray message; //!< Message received over the RF link
};

signals:
void dataReceived(const DataReceived);

}


Here the code where the signal is emitted


DataReceived packetReceived;
[...]
emit dataReceived(packetReceived) ;


The error:


error: no match for call to ‘(const QByteArray)(MatrixModule::DataReceived&)’


Where is the problem?

In my code there is also an another class with he signal


void dataByteReceived(const QByteArray &); //!< Data Available from the QextSerialPort


that is connected with the method that I've reported upper with the emission of the second signal after a data processing of the data provided by the serial port.

mattc
18th July 2009, 14:31
can you post the code connecting signals and slots?

PaceyIV
18th July 2009, 14:35
Actualy the signal dataReceived(const DataReceived); is connected to nothing.

I only connected the dataByteReceived(const QByteArray &) to the slot the parsering the data provided by the serial port and emmited the first signal in this way



connect(d_receiveThread, SIGNAL(dataByteReceived(const QByteArray &)),
this, SLOT(processDataReceived(const QByteArray &)));


The error is associated at the line where emits my signal with my structure data.

mattc
18th July 2009, 15:18
I'm puzzled too... the error seems unrelated to the emit :confused:

PaceyIV
18th July 2009, 15:31
If I comment only the line where I emit the signal, I've got no compile error.

Anyway I attach all my source code.

I don't know what to do :crying:

PaceyIV
18th July 2009, 15:49
I try to change the signal with a very simple one



void dataReceived(const QByteArray &);



and I emit this whit this code



QByteArray prova("Try!");

emit dataReceived(prova);


I can use this in my ReadingThread but in the MatrixModule I've got this error:

error: no match for call to ‘(const QByteArray) (const QByteArray&)’


Where is the real problem???

PaceyIV
18th July 2009, 15:57
I fix!
In the slot where I emit the signal dataReceived(..) there was a QByteArray dataReceived.