I am trying to use QProcess to interact with a command line application to retrieve data.

The application works something like this:

Qt Code:
  1. > execute_application
  2.  
  3. 1. open database
  4. 2. close database
  5. 3. read record
  6. 4. exit
  7.  
  8. Choose Option: 1
  9. Enter file name: blah.dat
  10.  
  11. 1. open database
  12. 2. close database
  13. 3. read record
  14. 4. exit
  15.  
  16. Choose Option: 3
  17. Enter ID#: 2365
  18.  
  19. Record: 2365
  20. data1 ####
  21. data2 ####
  22. data3 ####
  23. data4 ####
  24.  
  25.  
  26. 1. open database
  27. 2. close database
  28. 3. read record
  29. 4. exit
  30.  
  31. Choose Option: 4
  32.  
  33. >
To copy to clipboard, switch view to plain text mode 

My code goes something like this
Qt Code:
  1. QString line= "", command = "command";
  2. QProcess *proc = new QProcess(this);
  3. proc->start(command);
  4. proc->waitForStarted();
  5. proc->write("1\n");
  6. while(!line.contains("Select function")) line = tade->readAll();
  7. proc->write("blah.dat\n");
  8. while(!line.contains("Select function")) line = tade->readAll();
  9. proc->write("3\n");
  10.  
  11. ...
To copy to clipboard, switch view to plain text mode 

I seem to get in consistent results. Sometimes the variable line is always empty, sometimes it contains a string but it is from two commands ago, sometimes it only contains part of the menu and the rest of the menu appears in the next read call. I have tried using "waitForBytesWritten() and waitForReadReady()" but both always seem to return immediately eventhough some of options can take up to 10 seconds to return. Sometimes adding a wait... will cause one command to return properly while it has to be removed for the next command for it to work.

Some of this seems like it could be a buffering issue so I tried the unbuffered option but this seemed to make no difference. I also tried to increase the timeout on the wait.... functions but that seemed to make no difference.

I'd like to do the following:
1. start program
2. send command
3. wait for output (next input prompt)
4. save output
5. repeat 2-4