I am learning how to use QProcess to communicate with cmd.exe on WinXP. I wrote the following code, to send and receive messages and commands from my app to cmd.exe. This is a console application, not useful at all I know. I just wrote the simplier code possible to show you the error:

Qt Code:
  1. #include <QByteArray>
  2. #include <QProcess>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc,char** argv)
  9. {
  10. QProcess cmd;
  11. cmd.start("cmd");
  12. if (!cmd.waitForStarted())
  13. return false;
  14. cmd.waitForReadyRead();
  15. QByteArray result = cmd.readAll();
  16. cout << result.data();
  17.  
  18. string str;
  19. getline(cin,str);
  20. while(str != string("exit"))
  21. {
  22. cmd.write(str.c_str());
  23. cmd.write("\n");
  24. cmd.waitForReadyRead();
  25. result = cmd.readAll();
  26. cout << result.data();
  27. getline(cin,str);
  28. }
  29. }
To copy to clipboard, switch view to plain text mode 

Everything works well, except that the commands I send are delayed. For instance:

1) I launch my console application, this appears:

Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
2) I write "help" then press enter. Now the prompt looks like that:

Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
help
3) Then I press enter again, and my prompt displays:

Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
help
Pour plus d'informations sur une commande spécifique, entrez le nom de la commande HELP.
ASSOC Affiche ou modifie les applications associées aux extensions de...
It prints the cmd help, but only when I press enter twice. I can't figure out why there is this delay.

When I press enter my program is at the line 24 and wait to be able to read the cmd.exe output (cmd.waitForReadyRead(). When it is possible, result takes this output and prints it to my console application. I don't understand why result prints "help" and not the actual help of cmd.

Thanks in advance I hope I was clear