QProcess fails to execute external executable

会有一股神秘感。 提交于 2019-12-01 17:39:16

问题


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

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.

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

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.

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);

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

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

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

Hope someone can help me.

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

EDIT: Here the HelloWorld source.

#include <iostream>
using namespace std;

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

    return 0;
}

回答1:


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

  • start
  • execute
  • startDetached

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

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.

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().

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");


来源:https://stackoverflow.com/questions/18074826/qprocess-fails-to-execute-external-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!