Frage

I compiled a c++ source file from the Qt app I am creating. Now I want to run the exe file generated and also to redirect its input and output to txt files. But when I try to run it from QProcess, it fails to execute with exit code -2.

This is how I compiled the file using QProcess -

arguments << fileName << "-o" << exeFileName << "-static";
connect(compileProcess, SIGNAL(finished(int)), this, SLOT(compiled()));
compileProcess->start(QString("g++"), arguments);

And this is how I run the exe from QProcess in the slot compiled() -

runProcess->setStandardInputFile(inputFilename);
runProcess->setStandardOutputFile(QFileInfo(exeFileName).path() + "/output.txt");
int code = runProcess->execute(exeFileName); //code = -2

The program runs fine when I start it manually. So, why can't it be started from QProcess? I am working with Qt 5.0.2 on Windows 7

This is the source file I am compiling -

#include <iostream>

int main() {
    std::string s;s
    std::cin >> s;
    std::cout << s;
    return 0;
}
War es hilfreich?

Lösung

I finally got it to work. The exe file path had spaces in it and Qt did not implicitly add quotes around it. Adding quotes explicitly did the job.

runProcess->start("\"" + exeFileName + "\"");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top