QProcess 无法执行外部可执行文件 [英] QProcess fails to execute external executable

查看:53
本文介绍了QProcess 无法执行外部可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找解决问题的方法,但我根本不知道如何解决它.

I am struggling to find a solution to my problem, but I simply have no clue how to solve it.

我正在为我制作的一些程序创建一个用户界面(因此您只需按一个按钮即可启动可执行文件).于是想到用qt.

I am creating an user-interface for some programs I made (so you can through simply pressing a button start an executable). So I thought of using qt.

所以我阅读了很多关于 QProcess 的内容并尝试使用它.

So I read a lot about the QProcess and tried to use it.

在我的第一个可执行文件中,我尝试通过 QProcess::start() 启动它,但它不起作用,所以我尝试使用 QProcess:execute():

At the first executable of mine I tried to start it through QProcess::start(), but it didn't work so I tried it with QProcess:execute():

QProcess *proc = new QProcess(this);
QDir::setCurrent("C:\\DIRTOTHEEXE\\");
QString program="HELLO.exe";
proc->execute(program);

这完美地执行了我的程序并且运行良好.

This executes my program perfectly and works nice.

所以我尝试对我的其他 exe 执行相同的操作,但没有成功

So I tried to do the same with my other exe, but it didn't work

QProcess *myproc = new QProcess(this);
QDir::setCurrent("C:\\DIRTOTHEEXE\\");
QString program="HelloWorld.exe";
myproc->start(program);

被调用的可执行文件只是简单地打印Hello World"然后返回 0.

The called executable simply prints "Hello World" and returns 0 then.

所以现在我的问题是:什么可能导致这种行为,为什么我不能将 QProcess::start() 用于第一个可执行文件?

So now my question is: What could cause this behaviour and why can't I use QProcess::start() for the first executable?

顺便说一句:我也尝试将workingDirectory() 设置为exe 的路径,但也没有用.

Btw: I also tried to set the workingDirectory() to the path of the exe, but also that didn't work.

希望有人能帮助我.

所以程序被执行了,但在打印出一行后立即崩溃.

So the program is executed but crashes right after printing out one line.

这里是 HelloWorld 源.

Here the HelloWorld source.

#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    cout<<"HELLO WORLD!!"<<endl;

    return 0;
}

推荐答案

QProcess 有 3 个启动外部进程的函数,例如:-

QProcess has 3 functions for starting external processes, such as: -

  • 开始
  • 执行
  • 开始分离

后两者 execute 和 startDetached 是静态的,所以不需要 QProcess 的实例来调用它们.

The latter two, execute and startDetached are static, so don't need an instance of QProcess to call them.

如果你使用 start,你至少应该调用 waitForStarted() 来让进程正确设置.execute() 函数将等待进程完成,因此不需要调用 waitForStarted.

If you use start, you should at least be calling waitForStarted() to let the process setup properly. The execute() function will wait for the process to finish, so calling waitForStarted is not required.

由于您只发布了少量代码,我们无法确切了解您之后要做什么.该代码是否在结束的函数中,或者您是否试图检索该过程的输出?如果是这样,你肯定应该在使用 start() 时调用 waitForStarted.

As you've only posted a small amount of code, we can't see exactly what you're trying to do afterwards. Is that code in a function that ends, or are you trying to retrieve the output of the process? If so, you definitely should be calling waitForStarted if using start().

如果你只想运行进程而不等待它完成并且你的程序不关心与进程交互,那么使用 startDetached:-

If you only want to run the process without waiting for it to finish and your program is not bothered about interacting with the process, then use startDetached: -

QProcess::startDetached("C:\\DIRTOTHEEXE\\HELLO.exe");

这篇关于QProcess 无法执行外部可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆