PDA

View Full Version : execution in QT



adamatic
19th February 2009, 11:57
Hello.

Which is the Qt behaviour respected to the program execution? I mean, when a signal is emitted and a slot executed in another class, is it running in parallel or is going to wait to finish the previous execution and then continue with the slot called?.

I ask this question because I have this case:

A for loop calls another function which emits a signal to send a mensage by a QSocket. My intension is to send the mesage and received an answer before continuing with the next step in the loop. Once the transmition and reception have finished continue with the for loop. But what is happenning is that the signal is emitted but the socket doesnt send it until the for loop which called have finished with all the steps.

It is difficult explain the problem, I hope you have understand me. I show you more or less the code to do it more easy.



.........
for(i=0;i<n;i++) {

proof(i);

}
........

connect(this, SIGNAL(send()), SLOT(writeMsg()));
connect(clienteSTTE->tcpSocket,SIGNAL(readyRead()),SLOT(leer()));

proof(int i){

emit send();

}

writeMsg{

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);


QString string = QString(QLatin1String(this->mensajeEnviar.mensaje)) ;
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << string;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));


if(tcpSocket->isWritable()){

tcpSocket->write(block);
}
}

jogeshwarakundi
19th February 2009, 12:04
your for loop only sends the data to the remote end.
by the time it returns from teh emit statement, the message would have been delivered to socket.
you should put a wait statement inside the forloop itself if you want to wait till the answer is received
refer to QAbstractSocket::waitForReadyRead() for more info...

cheers!

adamatic
20th February 2009, 07:46
I did the following chane, includeing the waits in the proof function, like this:


proof(int i){

tcpSocket->waitForBytesWritten(-1);
emit send();
tcpSocket->waitForReadyRead(-1);

}

Is it correct now?

jogeshwarakundi
20th February 2009, 07:57
did you check by execution?? ;)
do you want to read the answer also inside the proof function?
or is the actual reading of answer the responsibility of somebody else?

adamatic
20th February 2009, 08:12
No, I am actually reading from another class which contains the tcpSocket, but the idea it is that the function proof doesnt finish until the datas have been readen, so for this reason I wrote the waitForReadyRead there.

In the main function i have the declaraton of this connect:

connect(tcpSocket,SIGNAL(readyRead()),SLOT(leer()) );

When the datas are ready are readen and the function proof finish the execution.