QProcess能夠用來啓動外部程序,並與它們交互。javascript
要啓動一個進程,經過調用start()來進行,參數包含程序的名稱和命令行參數,參數做爲一個QStringList的單個字符串。java
另外,也能夠使用setProgram()和setArguments()來運行,而後調用start()或open()。sql
start() 啓動外部程序ruby
readAllStandardError() 從標準錯誤中獲取全部數據markdown
readAllStandardOutput() 從標準輸出中獲取全部數據網絡
write() 繼承於QIODeviceapp
close() 繼承於QIODevice函數
除此以外,QProcess還包含靜態成員函數:ui
execute() 啓動一個進程,而後等待該進程結束。this
startDetached() 啓動一個進程,而後使其和當前進程脫離進程的父子關係。
QProcess process(this);
process.startDetached("cmd.exe");
使用cmd來刪除本地文件
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來查看網絡情況
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();
QString program = "E:/Putty.exe";
QStringList arguments;
arguments<< "-pw" << "wang" << QString("%1@%2").arg("root").arg("172.18.5.73");
QProcess *process = new QProcess(this);
process->setProcessChannelMode(QProcess::SeparateChannels);
process->setReadChannel(QProcess::StandardOutput);
process->start(program, arguments, QIODevice::ReadWrite);
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(22);
QProcess *process = new QProcess(this);
process->setProcessChannelMode(QProcess::SeparateChannels);
process->setReadChannel(QProcess::StandardOutput);
process->start(program, arguments, QIODevice::ReadWrite);
一個進程的標準輸出流到目標進程的標準輸入。
command1 | command2
能夠用下面代碼實現:
QProcess process1;
QProcess process2;
process1.setStandardOutputProcess(&process2);
process1.start("command1");
process2.start("command2");
啓動外部程序,當發生錯誤時,能夠根據指定的錯誤描述所發生的錯誤類型。
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
void processError(QProcess::ProcessError error)
{
switch(error)
{
case QProcess::FailedToStart:
QMessageBox::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;
}
}
假設不存在對應的外部程序,則會返回錯誤類型QProcess::FailedToStart
。
以putty遠程登陸爲例,putty能夠使用命令行putty [-pw password] user@ip
來進行鏈接。
因此中間爲空格的地方使用arguments進行單個字符串分離:
QStringList arguments;
arguments<< "-pw" << "wang" << QString("%1@%2").arg("root").arg("172.18.5.73");
其它參數相似。
QProcess process;
process.start("del /s *.txt");
//等同於process.start("del", QStringList() << "/s" << "*.txt");
返回調用進程的環境變量做爲一個鍵值對列表。
QStringList environment = QProcess::systemEnvironment();
//environment = {"PATH=/usr/bin:/usr/local/bin", "USER=greg", "HOME=/home/greg"}
原文做者:一去丶二三裏
做者博客:去做者博客空間