Results 1 to 8 of 8

Thread: readLine in QProcess

  1. #1
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default readLine in QProcess

    Hi to all.
    I'm developing a small program that get some info from a command line tool (clt) and elaborate it.

    I'm using QProcess to start the clt and the signal readyReadStandardOutput() to execute the functin that read the std out of the clt.

    it works but don't read the last line.

    For example, if the clt write to out:

    first pass ok
    second pass ok
    third pass failed
    4th pass ok
    5th pass failed

    and the gui slot connected to readyReadStandardOutput is

    f(){
    QString strOut=Process->readLine();
    QMessageBox::information(this,"aaa",strOut);
    }

    the program show only 4 pupups with the first 4 lines(the popup with "5th pass failed" is not showed) .
    sometimes, there is a pause of 3-4 seconds between various output lines from the clt because some times it use the network to do the job.

    i'm doing someting wrong?

    Thanks for help, and sorry for english...


    EDIT: using qt 4.5.2 on windows xp
    Last edited by grisson; 14th October 2009 at 15:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readLine in QProcess

    Can you show a bit more of your code, in particular, when and how you are destroying you QProcess object?
    It could be you are destroying your QProcess object before it got a chance to send the readyReadStandardOutput() signal.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: readLine in QProcess

    here it is the code of the click slot of one button:

    Qt Code:
    1. void MainJackSMS::on_InviaSMS_clicked(){
    2. SendSmsProcess = new QProcess( this );
    3. QStringList arguments;
    4. arguments <<"-sms64"<<_smsText<<"-dest"<<_dest;
    5. arguments <<"-serv"<<_serv;
    6. connect(SendSmsProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadStdoutSendSms()));
    7. SendSmsProcess->start(JACKSMS_BINARY_FILENAME,arguments);
    8. }
    To copy to clipboard, switch view to plain text mode 
    (note: _smsText, _serv and _dest are results of some omitted functions)

    SendSmsProcess is declared in the declaration of the class ( into the header file)
    Qt Code:
    1. private:
    2. QProcess *SendSmsProcess;
    To copy to clipboard, switch view to plain text mode 

    the object pointed from SendSmsProcess is destroyed when the program is closed(actually for some test i don't destroy the object). it can be a problem?



    EDIT: during some tests today, i've writthen another line (now are 6 lines) from the clt to stdout and the qt gui has readed all the lines except the 6th line. afther that i've printed out with a loop 100 lines, but only 99 lines are readed from the gui. the problem is always the same.
    Last edited by grisson; 14th October 2009 at 20:34.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readLine in QProcess

    You are allocating a new QProcess every time on_InviaSMS_clicked() is being called, without destroying it afterwords - not good - your this is a memory leak.
    The best would be to initialize it once and start and close new process on the same object.
    Or, if you want to allocate a new object every time you need to delete it when you don't need it anymore.
    But I don't think this has to do with your problem.

    What happens if you add a '\n' to the last string you send?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Angry Re: readLine in QProcess

    Quote Originally Posted by high_flyer View Post

    What happens if you add a '\n' to the last string you send?
    nothing, all the strings are already sended with the '\n' char...

    i've tried another way.
    i've edited the ReadStdoutSendSms() functions.
    now is like this:

    Qt Code:
    1. ReadStdoutSendSms(){
    2.  
    3. for (int i=0;i<5;i++){
    4. if (Process->canReadLine()){
    5. QString strOut=Process->readLine();
    6. QMessageBox::information(this,"aaa",strOut)
    7. }else{
    8. Sleep(10); //defined in windows.h
    9.  
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Works , but (obviously) it freeze the gui. i can try to do this in a different thread, , however i don't think it is a good solution...

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readLine in QProcess

    oh wait, so the original code was sending the 5 strings, and then what?
    Did the application end?
    If yes, then my original assumption (QProcess object being deleted before the it could read the cout) was *probably* correct.

    It looks to be a very not efficient way of doing things in your code, but its hard to say exactly what would be a better waz with out seeing more of it.

    You can have the gui responsive in your new code by adding QApplication:rocessEvents() in your loop.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Oct 2009
    Location
    Mantova, Italy
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: readLine in QProcess

    Quote Originally Posted by high_flyer View Post
    oh wait, so the original code was sending the 5 strings, and then what?
    Did the application end?
    If yes, then my original assumption (QProcess object being deleted before the it could read the cout) was *probably* correct.

    It looks to be a very not efficient way of doing things in your code, but its hard to say exactly what would be a better waz with out seeing more of it.

    You can have the gui responsive in your new code by adding QApplication:rocessEvents() in your loop.
    no.the original tool send an arbitrary number of lines. for a test i have limited the numer to 5 but it can be 1,2,3 or 10,20 etc... and after it sent the last line, the program terminate immediatly(and for this, your assumption seem to be plausible).

    the previous code was only a test to see if the gui receive all the lines.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: readLine in QProcess

    Well yes, if your application terminates immediately after sending the last string, then I strongly suspect it terminates before QProcess had a change to handle the last slot.

    Try to have the application not terminate, and see if this solves the problem.
    If you get all the strings when the application doesn't terminate, then you have a design problem.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  2. QProcess exitStatus()
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:51
  3. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  5. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30

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.