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

标签 c++ qt qprocess

我正在尝试在 Qt 中将外部可执行文件(下面的代码)作为单独的进程运行。

测试.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 中检查“程序”文件是否确实存在,然后再执行它。

从 main() 函数返回不同的结果代码并在 Qt 中检查结果:

QProcess *proc = new QProcess();

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

QString result=proc->readAllStandardOutput();

// Check result here

关于c++ - 使用 QProcess 在 Qt 中运行外部可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31174173/

相关文章:

c++ - Qt图表,系列不显示

c++ - Qprocess 弄乱了我的 linux 命令(我认为)。怎么修?

c++ - 如何确保 lrint 在 gcc 中内联?

c++ - 如何在 Clions 外部工具中使用 Emacs

c++ - 在 8 Puzzle | 中寻找曼哈顿距离的目标坐标C++

qt - QT 更新值信号/槽的最佳实践

c++ - 使用 libpqxx 连接到 Postgres 数据库

c++ - Qt windeployqt 失败,出现 : does not seem to be a Qt executable?

c++ - 暂停和恢复由 Qt 中的 QProcess 启动的子进程

c++ - 如何使用 Qt 运行 Windows cmd 命令?