5
votes

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
Hmmm. It looks like a program called HelloWorld would output 'Hello World' which is what you are seeing. - ExpatEgghead
Yes it outputs it, but it chrashes then (and asks if I want to send a report to ms (No idea how the report is called in english)). - jj01
Does helloworld.exe run by itself? - ExpatEgghead
Yes, I can start it and it doesn't crash, only when I execute it from Qt. And that makes it pretty hard for me to find the error, since I am not used to Qt at all. - jj01
What do you mean by At the first executable of mine I tried to start it through QProcess::start(), but it didn't work? Does that mean that nothing happened? QProcess::execute() calls QProcess::start() so it should work. - thuga

1 Answers

6
votes

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