简体   繁体   中英

Running external executable in Qt using QProcess

I'm trying to run an external executable (code below) in Qt as a separate process.

test.c:

#include <stdio.h>
int main () {
    FILE *f;
    f = fopen("a.txt", "w");
    fprintf(f, "1\n");
    fclose(f);
    return 1;
}

and in Qt I have:

QProcess* process = new QProcess();
QString program = "/Users/myUser/Desktop/a.out";
process->execute(program);

I've read up on the differences between execute(), start(), and startDetached() and to my understanding I want to use execute() because I want the process running the external executable to finish before continuing execution in the main process. However I've tried all three expecting to find a file a.txt containing the text "1" in it, but it doesn't exist. Any help or suggestions as to why it's not working? Thanks!

Check in the main() -function that a.txt -file really exists and is open before writing to it.

Check in the Qt that the "program" -file really exists before executing it.

Return different result codes from the main() -function and check the result in Qt:

QProcess *proc = new QProcess();

proc->start(program);
proc->waitForFinished();

QString result=proc->readAllStandardOutput();

// Check result here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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