简体   繁体   English

QProcess如何在Windows上运行

[英]How does QProcess work on windows

I'm trying to learn how QProcess works and have this kind of code: 我正在尝试学习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

The process "program" should be run on a the folder "H:\\sample" which has two files, main.cpp and Makefile. 进程“程序”应该在文件夹“H:\\ sample”上运行,该文件夹有两个文件main.cpp和Makefile。

My expectation is that "make" will be invoked with the "all" argument. 我的期望是“make”将使用“all”参数调用。 Examining the output of the process (in the file "H:\\processOutput.txt") i only see the text "main" and there is no any output of compilation. 检查进程的输出(在文件“H:\\ processOutput.txt”中)我只看到文本“main”,并且没有任何编译输出。

Running "make all" on cmd works and yield usual results, main.exe. 在cmd上运行“make all”工作并产生通常的结果,main.exe。 The whole code seem run to the end because i can see the line "done...". 整个代码似乎运行到底,因为我可以看到“完成......”这一行。 What am I missing? 我错过了什么?

QProcess, as the name indicates, starts a separate process, however the process is not bound to an environment map the same way command prompt is. 正如名称所示,QProcess启动一个单独的进程,但是进程没有像命令提示符那样绑定到环境映射。

Since there is no executable make in H:\\sample the process quits immediately. 由于H:\\sample没有可执行的make ,因此进程立即退出。 Instead, wrap your call around cmd like this: 相反,将您的调用包围在cmd中,如下所示:

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

%cmdspec% is a global environmental variable that indicates the default system path to command prompt executable. %cmdspec%是一个全局环境变量,指示命令提示符可执行文件的默认系统路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM