简体   繁体   English

使用QProcess在Qt中运行外部可执行文件

[英]Running external executable in Qt using QProcess

I'm trying to run an external executable (code below) in Qt as a separate process. 我试图在Qt中将外部可执行文件(下面的代码)作为一个单独的进程运行。

test.c: 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: 在Qt中,我有:

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. 我已经阅读了execute(),start()和startDetached()之间的区别,并且据我了解,我想使用execute(),因为我希望运行外部可执行文件的过程在继续执行主过程之前完成。 However I've tried all three expecting to find a file a.txt containing the text "1" in it, but it doesn't exist. 但是,我尝试了所有这三个方法,希望找到其中包含文本“ 1”的文件a.txt,但是它不存在。 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. 在写入文件之前,先检查main()函数是否确实存在a.txt文件并已打开。

Check in the Qt that the "program" -file really exists before executing it. 在执行Qt之前,先检查Qt中“ program”文件是否确实存在。

Return different result codes from the main() -function and check the result in Qt: 从main()函数返回不同的结果代码,并在Qt中检查结果:

QProcess *proc = new QProcess();

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

QString result=proc->readAllStandardOutput();

// Check result here

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

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