Results 1 to 2 of 2

Thread: QProcess cmd

  1. #1
    Join Date
    Jan 2008
    Posts
    3

    Default QProcess cmd

    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

  2. #2
    Join Date
    Jan 2008
    Posts
    3

    Default Re: QProcess cmd

    Oh my bad! Sorry in fact I just have to wait to cmd to send the help content without press enter again. I was just too hurry.

    But I still can't see why the word that I type (here the word "help") reapears below:

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

    [path_to_my_current_dir]>
    help
    It's better with a picture:
    I type "help" on the white line and after it looks like that:


    The green QTextEdit only receive data with the function cmd.readAll(); cmd is a QProcess. So it shouldn't receive the word "help" but only the help content that follows.

Similar Threads

  1. QProcess start a console program
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 12:38
  2. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  3. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  4. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.