5

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;
}
jj01
  • 68
  • 1
  • 7
  • Hmmm. It looks like a program called HelloWorld would output 'Hello World' which is what you are seeing. – ExpatEgghead Aug 06 '13 at 08:12
  • 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 Aug 06 '13 at 08:15
  • Does helloworld.exe run by itself? – ExpatEgghead Aug 06 '13 at 08:24
  • 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 Aug 06 '13 at 08:32
  • 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 Aug 06 '13 at 08:33
  • Yes I tried start, it simply prints out my qDebug() states but doesn't do anything of my executable, but wenn I use execute it does it. I have no idea why.. – jj01 Aug 06 '13 at 08:46
  • I tried it now with startDetached, there I see the outputs of both exes but the hello world exe crashes. So that start doesn't work is probably not true (sorry,my bad) but it doesn't print anything to the console of qt. – jj01 Aug 06 '13 at 08:51
  • Are you doing this in main? – thuga Aug 06 '13 at 08:54
  • No in the clicked functions. – jj01 Aug 06 '13 at 08:56
  • did you add `proc->waitForFinished();` after you have started the process? – Shf Aug 06 '13 at 09:01
  • Added it now, but it still crashes. – jj01 Aug 06 '13 at 09:04

1 Answers1

6

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");
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • My plan is to simply execute the exe so far by pressing the buttons. – jj01 Aug 06 '13 at 08:53
  • And the executable should finish since later it is going to create a new document (By pressing the button, it should call an executable which opens an existing file, changes it and stores the changes in an new file) – jj01 Aug 06 '13 at 08:54
  • 1
    As well as waitForStarted, there's also a waitForFinished function. Also, if you instantiate a QProcess object, ensure that you connect a slot to its error signal. – TheDarkKnight Aug 06 '13 at 08:55
  • Okay, but if I use "QProcess::startDetached("C:\\DIRTOTHEEXE\\HELLO.exe");" it doesn't do anything. Hmm But great to know about the functions waitForStarted() and waitForFinished() for the later implementation(creating a new file). – jj01 Aug 06 '13 at 09:01
  • 1
    If startDetached isn't working, then there's likely something wrong with how you're passing the path to the executable. – TheDarkKnight Aug 06 '13 at 09:37
  • Well when I call startDetached with an instance of QProcess, it works fine, but helloworld still crashes, though the other executable doesn't. – jj01 Aug 06 '13 at 09:48
  • Oh, you're saying that the 'helloworld' program crashes, not the program that is calling it with QProcess? – TheDarkKnight Aug 06 '13 at 10:09
  • Well helloworld prints out Hello World!! and then the process of helloworld crashes, though it works when I call it myself. – jj01 Aug 06 '13 at 10:31
  • Added it to the original post – jj01 Aug 06 '13 at 11:05
  • It seems strange that it should fail. Can you try running on a different machine and confirm if it does the same? – TheDarkKnight Aug 06 '13 at 14:46
  • Great idea, I will try to do it today. – jj01 Aug 07 '13 at 04:55
  • Okay somehow the problem is gone.. I have actually no idea why, I simply created a new project and wrote everything new and now it worked. Thanks for the help, if I find the reason, why it didn't work, I will post it here right away. – jj01 Aug 07 '13 at 06:35