Results 1 to 5 of 5

Thread: execution in QT

  1. #1
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default execution in QT

    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.

    Qt Code:
    1. .........
    2. for(i=0;i<n;i++) {
    3.  
    4. proof(i);
    5.  
    6. }
    7. ........
    8.  
    9. connect(this, SIGNAL(send()), SLOT(writeMsg()));
    10. connect(clienteSTTE->tcpSocket,SIGNAL(readyRead()),SLOT(leer()));
    11.  
    12. proof(int i){
    13.  
    14. emit send();
    15.  
    16. }
    17.  
    18. writeMsg{
    19.  
    20. QByteArray block;
    21. QDataStream out(&block, QIODevice::WriteOnly);
    22.  
    23.  
    24. QString string = QString(QLatin1String(this->mensajeEnviar.mensaje)) ;
    25. out.setVersion(QDataStream::Qt_4_0);
    26. out << (quint16)0;
    27. out << string;
    28. out.device()->seek(0);
    29. out << (quint16)(block.size() - sizeof(quint16));
    30.  
    31.  
    32. if(tcpSocket->isWritable()){
    33.  
    34. tcpSocket->write(block);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: execution in QT

    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!
    Let your work talk for you

  3. #3
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: execution in QT

    I did the following chane, includeing the waits in the proof function, like this:

    Qt Code:
    1. proof(int i){
    2.  
    3. tcpSocket->waitForBytesWritten(-1);
    4. emit send();
    5. tcpSocket->waitForReadyRead(-1);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is it correct now?

  4. #4
    Join Date
    Dec 2007
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: execution in QT

    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?
    Let your work talk for you

  5. #5
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: execution in QT

    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.

Similar Threads

  1. Replies: 6
    Last Post: 25th December 2008, 05:58
  2. Replies: 12
    Last Post: 9th April 2008, 13:49
  3. Qtopia example execution error(application hangs)
    By devendra in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th October 2006, 09:11
  4. Threads creatoin and execution?
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 23rd March 2006, 11:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.