Brief Introduction
Qprocess can be used to start external programs and interact with them.
To start a process by calling start (), the parameter contains the name of the program and the command-line arguments, as a single string of qstringlist.
Alternatively, you can use Setprogram () and setarguments () to run, and then call Start () or open ().
Brief interface Example cmd start cmd cmd with parameter cmd get return value putty telnet WINSCP remote file transfer pipeline error handling parameter arguments get environment variable
Interface
Start () Launch external program
Readallstandarderror () Get all the data from the standard error
Readallstandardoutput () Get all data from standard output
Write () inherits from Qiodevice
Close () inherits from Qiodevice
In addition to this, qprocess also contains static member functions:
Execute () starts a process, and then waits for the process to end.
Startdetached () starts a process and then makes it out of the process's parent-child relationship with the current process.
Example cmd start cmd
Qprocess process (this);
Process.startdetached ("cmd.exe");
cmd with Parameters
Use cmd to delete local files
Qprocess process (this);
Process.Start ("cmd.exe");
Process.write ("del e:\\a.txt\n\r");
Process.write ("exit\n\r");
Process.waitforfinished ();
Process.close ();
cmd Gets the return value
Use CMD to view network conditions
qstringlist arguments;
Arguments << "/C" << "ping www.baidu.com";
Qprocess process (this);
Process.Start ("cmd.exe", arguments);
Process.waitforstarted ();
Process.waitforfinished ();
QString strresult = Qstring::fromlocal8bit (Process.readallstandardoutput ());
Qmessagebox MsgBox (this);
Msgbox.settext (strresult);
Msgbox.exec ();
Putty Telnet
QString program = "E:/putty.exe";
qstringlist arguments;
arguments<< "-PW" << "Wang" << QString ("%1@%2"). Arg ("root"). Arg ("172.18.5.73") << "a";
Qprocess *process = new Qprocess (this);
Process->setprocesschannelmode (qprocess::separatechannels);
Process->setreadchannel (qprocess::standardoutput);
Process->start (program, arguments, Qiodevice::readwrite);
winscp Remote File transfer
QString program = Qcoreapplication::applicationdirpath () + "/winscp/winscp.exe";
qstringlist arguments;
Arguments << QString ("%1:%2@%3:%4"). Arg ("root"). Arg ("Wang"). Arg ("172.18.5.73"). Arg;
Qprocess *process = new Qprocess (this);
Process->setprocesschannelmode (qprocess::separatechannels);
Process->setreadchannel (qprocess::standardoutput);
Process->start (program, arguments, Qiodevice::readwrite);
Piping
A process's standard output stream to the standard input of the target process.
Command1 | Command2
Can be implemented with the following code:
Qprocess Process1;
Qprocess Process2;
Process1.setstandardoutputprocess (&PROCESS2);
Process1.start ("Command1");
Process2.start ("Command2");
Error Handling
Initiates an external program that, when an error occurs, can describe the type of error that occurred based on the specified error.
connect (Process, SIGNAL (Error (qprocess::P rocesserror)), this, SLOT (Processerror (qprocess
::P rocesserror))); void Processerror (qprocess::P rocesserror error) {switch (error) {case Qprocess::failedtostart:qmessa
Gebox::information (0, "Tip", "Failedtostart");
Break
Case qprocess::crashed:qmessagebox::information (0, "Tip", "Crashed");
Break
Case qprocess::timedout:qmessagebox::information (0, "Tip", "timedout");
Break
Case qprocess::writeerror:qmessagebox::information (0, "Tip", "Writeerror");
Break
Case qprocess::readerror:qmessagebox::information (0, "Tip", "Readerror");
Break
Case qprocess::unknownerror:qmessagebox::information (0, "Tip", "Unknownerror");
Break
Default:qmessagebox::information (0, "Tip", "Unknownerror");
Break }
}
Assuming there is no corresponding external program, the error type Qprocess::failedtostart is returned. parameter Arguments
Take Putty Telnet as an example, putty can use the command line putty [-PW password] user@ip to connect.
So where the middle spaces are separated by a single string using arguments:
qstringlist arguments;
arguments<< "-PW" << "Wang" << QString ("%1@%2"). Arg ("root"). Arg ("172.18.5.73");
Other parameters are similar.
Qprocess process;
Process.Start ("del/s *.txt");
Equivalent to Process.Start ("Del", Qstringlist () << "/S" << "*.txt");
Get Environment Variables
Returns the environment variable that invokes the process as a list of key-value pairs.
Qstringlist environment = qprocess::systemenvironment ();
Environment = {"Path=/usr/bin:/usr/local/bin", "User=greg", "Home=/home/greg"}