Results 1 to 6 of 6

Thread: Running external process like cmd.exe

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Running external process like cmd.exe

    Hi,
    I have problem with support / starting external process.
    I have simple console application which adds two entered numbers and display result.

    When I run this console application in cmd.exe it works fine - stdin in cmd.exe is directly send
    to my application, stdout in my application is directly send to cmd.exe console and when my app exit
    stdin return to work with cmd. sample use:

    testApp.png

    Now question - how i can set QProcess that can work the same way?

    what I have:
    Qt Code:
    1. QProcess process;
    2. process.start( "C:/Users/C50/Desktop/test/consoleQt.exe",
    3. QStringList(), QIODevice::ReadWrite | QIODevice::Text );
    4.  
    5. QTextStream in( stdin );
    6.  
    7. while( process.state() == QProcess::Running ) {
    8.  
    9. process.write( in.readLine().toStdString().c_str() );
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Running external process like cmd.exe

    Use signals, from the docs:
    (after calling QProcess::start) ... QProcess then enters the Starting state, and when the program has started, QProcess enters the Running state and emits started().
    You can also call waitForStarted() after start() if you prefer synchronous api.
    Also, there is no need for this conversion:
    Qt Code:
    1. process.write( in.readLine().toStdString().c_str() );
    To copy to clipboard, switch view to plain text mode 
    this will do:
    Qt Code:
    1. process.write(in.readLine());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running external process like cmd.exe

    I have some solution which work fine but I am not sure whether this code is ok and whether in some conditions can make problems.

    I found in documentation QProcess
    void QProcess::setInputChannelMode(InputChannelMode mode)

    QProcess::ForwardedInputChannel 1

    QProcess forwards the input of the main process onto the running process. The child process reads its standard input from the same source as the main process. Note that the main process must not try to read its standard input while the child process is running
    and
    QProcess::ForwardedChannels 2

    QProcess forwards the output of the running process onto the main process. Anything the child process writes to its standard output and standard error will be written to the standard output and standard error of the main process.
    It is my code
    Qt Code:
    1. process.setInputChannelMode( QProcess::ForwardedInputChannel );
    2. process.setProcessChannelMode( QProcess::ForwardedChannels );
    3.  
    4. process.start( "D:/Projects/Qt projects/4programQt/build-consoleQt-Desktop_Qt_5_3_MinGW_32bit-Release/release/consoleQt.exe",
    5. QStringList(), QIODevice::ReadWrite | QIODevice::Text );
    6.  
    7. process.waitForStarted();
    8. while( process.waitForFinished( - 1 ) ) {
    9.  
    10. if( process.state() == QProcess::Running )
    11. command = in.readLine();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Now i don't need use write() function because child process automatically read from stdin main process.
    What do you think about this code?

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running external process like cmd.exe

    Forwarding the channels to the main process is a bad idea that will not help you solve your problem.

    What you need to do is communicate asynchronously through the input and output channels as you would with a socket. Connect the QProcess' error(), finished(), readyReadStandardError(), and readyReadStandardOutput() signals to slots in your program. You can then accumulate the data as it arrives on the two channels and decode it either incrementally -- this requires keeping a potentially complex state around -- or in one go after the process has finished. If you want to give the illusion of synchrony in your program, use a QEventLoop.

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running external process like cmd.exe

    Why forwarding the channels to the main process is a bad idea?
    In tests it works fine. I can block main process until child working it is ok for me.

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running external process like cmd.exe

    Forwarding channels is a bad idea because you pollute your main program's channels with those of the child process. If the main program writes something on its output or error channel, you will end up trying to parse it. This is not just hypothetical: Qt's classes routinely print warning messages (though maybe only on the error channel, but who knows for sure?).

    In any case, this does not help you solve your problem: communicate with the child process. Your current solution should work equally well without forwarded channels, but you should realize that you may run into a deadlock. If the child process writes too much on its output/error channel and fills its internal buffer, it will wait for the main process to read the pending data. Since your main process currently starts reading only after the child process has terminated, you had better hope that the buffers are large enough.

Similar Threads

  1. use process to start an external makefile
    By knobby67 in forum Newbie
    Replies: 3
    Last Post: 31st July 2014, 14:50
  2. Deployment and external process question...
    By scott_hollen in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2011, 19:51
  3. Get Process ID for a running application
    By bob2oneil in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2011, 21:58
  4. QGraphicsView : paint from an external process
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2010, 12:01
  5. Destroyed while process is still running
    By qtzcute in forum Qt Programming
    Replies: 5
    Last Post: 23rd July 2009, 08:26

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.