qt

QProcess executing a c++ file


In some places it suggests, I can't run a pre-built c++ binary with QProcess. at the same time there are other questions where people are executing shell scripts etc with QProcess so I am confused. Can I execute a pre-built c++ binary using QProcess. This binary reads a text file and creates two text files in return. I created a basic UI with GUI and have a button which when clicked calls the external binary. Running this with execute gives me an error of QIODevice: read: device not open. When I use start, no errors are reported. But no output files are created either. Any ideas whether this is permitted in qt or some other approach needs to be followed.

void MainWindow::on_startButton_clicked()
{
QString program = "./home/naveen/sdj";
QProcess *myProcess = new QProcess(this);
myProcess->start(program);
myProcess->waitForFinished();
qDebug() << myProcess->exitStatus();
qDebug() << myProcess->readAllStandardError();
}        

Solution

  • First, QProcess::execute() is a static method -- there's no reason to create a QProcess instance to use it. If you use QProcess::start(), it will execute the process asynchronous. You have to listen for a finished signal before you can check for a return code.

    Second, are you sure this is what you intended?

    QString program = "./home/naveen/sdj";
    

    In *nix file systems, ./ means start in the current directory. So QProcess won't look for /home/naveen/sdj, it will instead look for /yourProjectBuildPath/home/naveen/sdj. I'm guessing that's not what you want.