PDA

View Full Version : emitting and catching signals



srohit24
2nd August 2009, 13:32
Hi

I have two classes called receive and write.

What i need is , a signal is emitted in the receive class. this signal has to be caught in the write class and proceed as instructed.

I am emitting a signal called finished_receiving after i have parsed the data I get from a website.
I want to connect this signal to a proceed function in the write class.

What all should i specify in the connect? how should I declare the sender and receiver objects?

faldzip
2nd August 2009, 15:25
Read about signals and slots in Qt Assistant or just click here (http://doc.qtsoftware.com/4.5/signalsandslots.html) as it is the link to online documentations, exactly the same which is in Assistant.

srohit24
2nd August 2009, 17:24
I have read that. I am able to emit signals and catch them in a single class.

I am facing problems when i need to emit and catch them in 2 different classes.

A bit of help in the direction of the solution will be greatly helpful.

PaceyIV
2nd August 2009, 21:20
Your class should be instantiate somewhere. There you should add
connect (classA, SIGNAL(signalA()), classB, SLOT(slotB()))
same as in the same class.

srohit24
3rd August 2009, 17:07
The two classes are classA and classB. The main class is declared as mainclass.

I am emitting a signal in classA. I have written the code for connect in mainclass's constructor
I have created a instance of the classA in the mainclass. The classA action isnt related to the mainclass action.

this is my connect signal


connect(classA_object,SIGNAL(receiving_finished()) , classB_object, SLOT(write_data()));

Its not giving me any errors but its not calling the write_data function either.

what am I doing wrong?

spirit
3rd August 2009, 17:16
are you sure that receiving_finished signal is emitted? put qDebug in code where you emits this signal.

nightghost
3rd August 2009, 20:17
It would be easier to detect the problem if you would provide more code.

srohit24
4th August 2009, 04:44
void api_request::parse_read_alldata()
{
.......
emit receiving_finished();
}//this is the classA function where i am emitting the signal

connect(classA_object,SIGNAL(receiving_finished()) , classB_object, SLOT(write_data()));
//This is present in the mainclass's constructor which has a instance of classB_object but no classA instance.

void write_data()
{
qDebug() << "in write data";
}//this is the actual function that needs to be called and is present in classB.

nish
4th August 2009, 04:48
connect(classA_object,SIGNAL(receiving_finished()) , classB_object, SLOT(write_data()));
//This is present in the mainclass's constructor which has a instance of classB_object but no classA instance.

you say the "no classA instance." is present in mainclass so how are you able to connect..
i am afraid that you need to give some more code... why not just provide a simple minimal compilable program reproducing the problem?

srohit24
4th August 2009, 04:55
Should i place the connect command in classB's constructor to connect the signals as classB has classA's instance?

EDIT

I placed the connect code in the classB's constructor.The app compiled successfully but i got this in qDebug



connect(classA_object,SIGNAL(receiving_finished()) , this, SLOT(write_data()));

QObject::connect: Cannot connect (null)::receiving_finished() to classB::write_data()

spirit
4th August 2009, 06:01
classA_object is not created, i.e. classA_object == 0.
EDIT: take a debugger in your hands and debug your programm step by step.

srohit24
4th August 2009, 17:19
ok. but I have created a class object and I am using it make a function call.

how can that be null?

spirit
4th August 2009, 18:32
this message says another


QObject::connect: Cannot connect (null)::receiving_finished() to classB::write_data()