小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Qt之啟動外部程序

 昵稱52164085 2018-01-19

簡述

QProcess可以用來啟動外部程序,并與它們交互。

要啟動一個進程,通過調(diào)用start()來進行,參數(shù)包含程序的名稱和命令行參數(shù),參數(shù)作為一個QStringList的單個字符串。

另外,也可以使用setProgram()和setArguments()來運行,然后調(diào)用start()或open()。

接口

  • start() 啟動外部程序

  • readAllStandardError() 從標(biāo)準(zhǔn)錯誤中獲取所有數(shù)據(jù)

  • readAllStandardOutput() 從標(biāo)準(zhǔn)輸出中獲取所有數(shù)據(jù)

  • write() 繼承于QIODevice

  • close() 繼承于QIODevice

除此之外,QProcess還包含靜態(tài)成員函數(shù):

  • execute() 啟動一個進程,然后等待該進程結(jié)束。

  • startDetached() 啟動一個進程,然后使其和當(dāng)前進程脫離進程的父子關(guān)系。

示例

cmd

啟動cmd

QProcess process(this);
process.startDetached("cmd.exe");
  • 1
  • 2

cmd帶參數(shù)

使用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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

cmd獲取返回值

使用cmd來查看網(wǎng)絡(luò)狀況

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

putty遠(yuǎn)程登錄

QString program = "E:/Putty.exe";

QStringList arguments;
arguments<< "-pw" << "wang" << QString("%1@%2").arg("root").arg("172.18.5.73") << "22";

QProcess *process = new QProcess(this);
process->setProcessChannelMode(QProcess::SeparateChannels);
process->setReadChannel(QProcess::StandardOutput);
process->start(program, arguments, QIODevice::ReadWrite);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

WinSCP遠(yuǎn)程文件傳輸

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

管道

一個進程的標(biāo)準(zhǔn)輸出流到目標(biāo)進程的標(biāo)準(zhǔn)輸入。

command1 | command2

可以用下面代碼實現(xiàn):

QProcess process1;
QProcess process2;

process1.setStandardOutputProcess(&process2);

process1.start("command1");
process2.start("command2");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

錯誤處理

啟動外部程序,當(dāng)發(fā)生錯誤時,可以根據(jù)指定的錯誤描述所發(fā)生的錯誤類型。

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

假設(shè)不存在對應(yīng)的外部程序,則會返回錯誤類型QProcess::FailedToStart。

參數(shù)arguments

以putty遠(yuǎn)程登錄為例,putty可以使用命令行putty [-pw password] user@ip來進行連接。

所以中間為空格的地方使用arguments進行單個字符串分離:

QStringList arguments;
arguments<< "-pw" << "wang" << QString("%1@%2").arg("root").arg("172.18.5.73");
  • 1
  • 2

其它參數(shù)類似。

QProcess process;
process.start("del /s *.txt");
//等同于process.start("del", QStringList() << "/s" << "*.txt");
  • 1
  • 2
  • 3

獲取環(huán)境變量

返回調(diào)用進程的環(huán)境變量作為一個鍵值對列表。

QStringList environment =  QProcess::systemEnvironment();
//environment = {"PATH=/usr/bin:/usr/local/bin", "USER=greg", "HOME=/home/greg"}
  • 1
  • 2

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多