Results 1 to 3 of 3

Thread: QProcess, problem getting interactive outputs

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QProcess, problem getting interactive outputs

    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
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QProcess, problem getting interactive outputs

    I think there only one way to solve your problem, konwing the reply length, syntax, format etc.

    1. Write Command
    2. Wait ultill you receive command reply, you need parse the reply (you need to know the format, length, syntax of the reply), and based on the reply received do step 3
    3. Write next Command / Data
    4. Read the reply, parse it and decide next step.
    .....

    You need to know the format / syntax of the reply, read the reply and make sure you have read the complete reply, (as you know the reply format), then write next command / data

    If don't know the reply format, length, syntax etc, I don't think you can fix this.

    As always waiting between commands for pre-defined time may help, and again if you plan to run large number of commands in a batch, then this will take long time and will be anoying.

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess, problem getting interactive outputs

    I am still trying to understand why, but I found that if I add "waitForReadReady()" into the loop where I look for the prompt it works. waitForReadReady() seems to allow me to read more from the output
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

Similar Threads

  1. cout<<5/2 outputs a integer?
    By el33t in forum General Programming
    Replies: 2
    Last Post: 22nd February 2011, 01:44
  2. Interactive QProcess
    By inktomi in forum Qt Programming
    Replies: 3
    Last Post: 8th August 2009, 18:21
  3. qsqlquery interactive?
    By lamera in forum Newbie
    Replies: 2
    Last Post: 7th September 2008, 10:09
  4. No Interactive feel-QProcess
    By prasann87 in forum Qt Programming
    Replies: 5
    Last Post: 11th January 2008, 10:47
  5. QT 4.2.3 - Size of compiled outputs
    By Declan in forum Installation and Deployment
    Replies: 1
    Last Post: 9th June 2007, 11:29

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.