c++ - QProcess 如何在 Windows 上工作

标签 c++ qprocess

我正在尝试了解 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,顾名思义,启动一个单独的进程,但是该进程不像命令提示符那样绑定(bind)到环境映射。

由于 H:\sample 中没有可执行的 make 进程会立即退出。相反,像这样围绕 cmd 调用:

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

%cmdspec% 是一个全局环境变量,指示命令提示符可执行文件的默认系统路径。

关于c++ - QProcess 如何在 Windows 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7970835/

相关文章:

c++ - C/C++ - 预编译 header - 封装、如何以及为什么需要配置?

c++ - 这个 char 数组到 std::string 的转换有什么问题?

c++ - QProcess:启动子进程而不等待完成但收到完成信号

qt - 为什么QProcess信号readyReadStandardOutput()发出两次?

c++ - QProcess::startDetached 的路径要求

c++ - 如何将 string.erase() 与指针一起使用?

C++ "group where"算法

c++ - 关于 move 一个 const 对象

python - 如何在 PySide 中获取 QProcess 运行的命令的输出?

qt - 如何启动分离进程并等待父进程终止?