运行 使用 QProcess 在 Qt 中的外部可执行文件
Running external executable in Qt using QProcess
我正在尝试 运行 Qt 中的外部可执行文件(下面的代码)作为一个单独的进程。
test.c:
#include <stdio.h>
int main () {
FILE *f;
f = fopen("a.txt", "w");
fprintf(f, "1\n");
fclose(f);
return 1;
}
在 Qt 中我有:
QProcess* process = new QProcess();
QString program = "/Users/myUser/Desktop/a.out";
process->execute(program);
我已经阅读了 execute()、start() 和 startDetached() 之间的区别,据我所知,我想使用 execute(),因为我希望进程 运行 宁外部可执行文件在主进程中继续执行之前完成。但是,我已经尝试了所有三个,希望找到一个包含文本“1”的文件 a.txt,但它不存在。关于为什么它不起作用的任何帮助或建议?谢谢!
在 main() 函数中检查 a.txt 文件确实存在并且在写入之前打开。
在 Qt 中检查 "program" 文件是否确实存在,然后再执行它。
Return 来自 main() 函数的不同结果代码并检查 Qt 中的结果:
QProcess *proc = new QProcess();
proc->start(program);
proc->waitForFinished();
QString result=proc->readAllStandardOutput();
// Check result here
我正在尝试 运行 Qt 中的外部可执行文件(下面的代码)作为一个单独的进程。
test.c:
#include <stdio.h>
int main () {
FILE *f;
f = fopen("a.txt", "w");
fprintf(f, "1\n");
fclose(f);
return 1;
}
在 Qt 中我有:
QProcess* process = new QProcess();
QString program = "/Users/myUser/Desktop/a.out";
process->execute(program);
我已经阅读了 execute()、start() 和 startDetached() 之间的区别,据我所知,我想使用 execute(),因为我希望进程 运行 宁外部可执行文件在主进程中继续执行之前完成。但是,我已经尝试了所有三个,希望找到一个包含文本“1”的文件 a.txt,但它不存在。关于为什么它不起作用的任何帮助或建议?谢谢!
在 main() 函数中检查 a.txt 文件确实存在并且在写入之前打开。
在 Qt 中检查 "program" 文件是否确实存在,然后再执行它。
Return 来自 main() 函数的不同结果代码并检查 Qt 中的结果:
QProcess *proc = new QProcess();
proc->start(program);
proc->waitForFinished();
QString result=proc->readAllStandardOutput();
// Check result here