I am trying to figure out the use of QProcess. I did not look at the Qt doc. http://doc.qt.io/qt-4.8/qprocess.html
EXAMPLES OF THE PROBLEM.
Example 1: The code below works.
#include <QtCore/QCoreApplication>
output of the above code
Hello
F:\Dev_Qt\expControllingExtConsoleApps-build-desktop> ---End, bye----
The problem is that if I try to use ipconfig or 7zip this way using the Qprocess and cmd console, I cannot see any output from ipconfig or 7zip. I donโt know if something has been done, if something has been done, why donโt I see a way out? Below is the code below.
Example 2: Doesn't work. Unable to use ipconfig.
#include <QtCore/QCoreApplication> #include <QTextStream> #include <QByteArray> #include <QString> #include <QProcess> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTextStream qout(stdout); QProcess cmd2; cmd2.setWorkingDirectory("C:/Program Files/7-Zip"); //not needed in this example. cmd2.setReadChannel(QProcess::StandardOutput); cmd2.setProcessChannelMode(QProcess::MergedChannels); cmd2.start("cmd"); if (!cmd2.waitForStarted()) { qout << "Error: Could not start!" << endl; return false; } cmd2.waitForReadyRead(); QByteArray result = cmd2.readAll(); qout << result.data() << endl; //Console version info, etc. //My command cmd2.write("ipconfig"); cmd2.write("\n"); //Capture output of ipconfig command //DOES NOT WORK!! cmd2.waitForReadyRead(); while (! cmd2.atEnd()) { result = cmd2.readLine(); qout << result; result.clear(); } qout << endl; qout << "\n\n---end----" << endl; return a.exec(); }
The result is below, there is no information about the result of connecting ipconfig. No output from ipconfig is written at all.
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C: \ Program Files \ 7-Zip> ipconfig
--- end ----
It must be more like this (with ipconfig result).
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C: \ Documents and Settings \ Noname> IPCONFIG
Windows IP Address Configuration
Ethernet adapter local area network Connection:
Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.172.148.135 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.172.148.177
C: \ Documents and Settings \ noname>
Obviously, the output should have been slightly different than the above, but the connection information, which is the ipconfig output, should have been captured. Similarly, if I try to use 7zip through the cmd console ... I cannot see / write any 7zip output. So my question is: how can I use command line applications like ipconfig and 7zip through QProcess and cmd console and see the output of these applications?
Example 3: 7zip does not work
#include <QtCore/QCoreApplication> #include <QTextStream> #include <QByteArray> #include <QProcess> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTextStream qout(stdout); QProcess cmd2; cmd2.setWorkingDirectory("C:/Program Files/7-Zip"); cmd2.setReadChannel(QProcess::StandardOutput); cmd2.setProcessChannelMode(QProcess::MergedChannels); cmd2.start("cmd"); if (!cmd2.waitForStarted()) { return false; } //My Command cmd2.write("7z.exe"); cmd2.write("\n"); //Capture output of ipconfig command cmd2.waitForReadyRead(); QByteArray result; while (! cmd2.atEnd()) { result = cmd2.readLine(); qout << result; result.clear(); } qout << endl; qout << "\n\n---end----" << endl; return a.exec(); }
The conclusion is below. Does not show anything from 7zip.
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C: \ Program Files \ 7-Zip> 7z.exe
--- end ----
It is expected that the output will be performed line by line ...
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C: \ Documents and Settings \ noname> cd C: \ Program Files \ 7-Zip
C: \ Program Files \ 7-Zip> 7z.exe
7-Zip 9.15 beta Copyright (c) 1999-2010 Igor Pavlov 2010-06-20
Usage: 7z [...] [...] [& L; @listfiles ...>]
a: Add files to archive
b: Test d: delete files from the archive e: Extract files from the archive (without using the names directory) l: List of archive contents
t: Check archive integrity u: Update files to x: eXtract archive files with full paths
-ai [r [- | 0]] {@listfile |! wildcard}: Enable archives
-ax [r [- | 0]] {@listfile |! wildcard}: eXclude archives -bd: disable percentage
-i [r [- | 0]] {@listfile |! wildcard}: Include file names -m {Parameters}: compression method -o {Directory}: set Output directory -p {Password}: set password -r [- | 0]: subdirectories Recurse -scs {UTF-8 | WIN | DOS}: set the encoding for the list files -sfx [{name}]: create an SFX archive -si [{name}]: read data from stdin -slt: show technical information for the l (List) -so command: write data to stdout -ssc [-]: set sensitive case mode -ssw: compress shared files
-t {type}: Set archive type -u [-] [p #] [q #] [r #] [x #] [y #] [z #] [! newArchiveName]: Update options -v {Size} [b | k | m | g]: Creating volumes -w [{path}]: assign the working directory. Empty path means temporary directory
-x [r [- | 0]]] {@listfile |! wildcard}: eXclude filenames -y: accept Yes all requests
C: \ Program Files \ 7-Zip>