簡體   English   中英

QProcess如何在Windows上運行

[英]How does QProcess work on windows

我正在嘗試學習QProcess如何工作並擁有這種代碼:

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <QtCore/QCoreApplication>
#include <QStringList>
#include <QString>
#include <QProcess>
#include <QIODevice>

#define LINE cout << "\n=====================================\n" << endl;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    LINE;
    cout << "\nstarting process ..." << endl;

    QObject *parent;
    QString program = "make";
    QStringList arguments;
    arguments << "all";
    QProcess *process = new QProcess();

    QString outputFile = "H:\\processOutput.txt";
    process->setStandardOutputFile( outputFile, QIODevice::Append);
    process->setWorkingDirectory( "H:\\sample");
    process->start(program, arguments );

    cout << "\ndone..." << endl;
    LINE;

    return a.exec();
} // end main

進程“程序”應該在文件夾“H:\\ sample”上運行,該文件夾有兩個文件main.cpp和Makefile。

我的期望是“make”將使用“all”參數調用。 檢查進程的輸出(在文件“H:\\ processOutput.txt”中)我只看到文本“main”,並且沒有任何編譯輸出。

在cmd上運行“make all”工作並產生通常的結果,main.exe。 整個代碼似乎運行到底,因為我可以看到“完成......”這一行。 我錯過了什么?

正如名稱所示,QProcess啟動一個單獨的進程,但是進程沒有像命令提示符那樣綁定到環境映射。

由於H:\\sample沒有可執行的make ,因此進程立即退出。 相反,將您的調用包圍在cmd中,如下所示:

...
QString program = "%cmdspec%";
QStringList arguments;
arguments << "\\C" << "\"make all\"";
QProcess *process = new QProcess();
...

%cmdspec%是一個全局環境變量,指示命令提示符可執行文件的默認系統路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM