Amani
Amani

Reputation: 18123

How does QProcess work on windows

I'm trying to learn how QProcess works and have this kind of code:

#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.

My expectation is that "make" will be invoked with the "all" argument. 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.

Running "make all" on cmd works and yield usual results, main.exe. The whole code seem run to the end because i can see the line "done...". What am I missing?

Upvotes: 0

Views: 3736

Answers (1)

enticedwanderer
enticedwanderer

Reputation: 4346

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.

Since there is no executable make in H:\sample the process quits immediately. Instead, wrap your call around cmd like this:

...
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.

Upvotes: 2

Related Questions