28

I want to start an external program out of my QT-Programm. The only working solution was:

system("start explorer.exe");

But it is only working for windows and starts a command line for a moment.

Next thing I tried was:

QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
//process.execute(file); //i tried as well

But nothing happened. Any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
btzs
  • 1,048
  • 3
  • 14
  • 17
  • 1
    What thomas_b says. Additionally, connect to the finished() and error() signals and call errorString() in case of error to learn about what's going wrong. – Frank Osterfeld Oct 18 '13 at 06:59

4 Answers4

33

If your process object is a variable on the stack (e.g. in a method), the code wouldn't work as expected because the process you've already started will be killed in the destructor of QProcess, when the method finishes.

void MyClass::myMethod()
{
    QProcess process;
    QString file = QDir::homepath + "file.exe";
    process.start(file);
}

You should instead allocate the QProcess object on the heap like that:

QProcess *process = new QProcess(this);
QString file = QDir::homepath + "/file.exe";
process->start(file);
m7913d
  • 10,244
  • 7
  • 28
  • 56
tomvodi
  • 5,577
  • 2
  • 27
  • 38
  • 1
    Will the QProcess automatically deallocate itself, when the process has started? – user1767754 Oct 31 '14 at 07:39
  • 5
    No, it won't. If the `process` object is getting deleted, it kills the corresponding process. With the static method `QProcess::startDetached` it is possible to start a new process without carrying a `QProcess` instance around. – tomvodi Oct 31 '14 at 08:02
  • Is there another approach starting a process? I just want to fire a process and forget about it. – user1767754 Oct 31 '14 at 08:03
  • 8
    Yes, with the mentioned static method `QProcess:startDetached` – tomvodi Oct 31 '14 at 08:13
  • 1
    Oh this is really hidden, i've asked this question couple of minutes ago, maybe you can send your answer there, then it is available for all. http://stackoverflow.com/questions/26670147/qt-start-external-process-fire-and-forget – user1767754 Oct 31 '14 at 08:19
8

If you want your program to wait while the process is executing and only need to get its exit code, you can use

QProcess::execute(file);
QProcess::exitCode(); // returns the exit code

instead of using process asynchronously like this.

QProcess process;
process.start(file);

Note that you can also block execution until process will be finished. In order to do that use

process.waitForFinished();

after start of the process.

Community
  • 1
  • 1
nv95
  • 241
  • 2
  • 3
  • This is an option worth considering. [Execute](http://doc.qt.io/qt-5/qprocess.html#execute-1) is a convenience function that executes a process and waits for it to finish. It's static though. You don't call it on a `QProcess` instance. – cgmb Nov 17 '15 at 17:32
5

QDir::homePath doesn't end with separator. Valid path to your exe

QString file = QDir::homePath + QDir::separator + "file.exe";
nnesterov
  • 1,232
  • 1
  • 10
  • 27
5

Just use QProcess::startDetached; it's static and you don't need to worry about waiting for it to finish or allocating things on the heap or anything like that:

QProcess::startDetached(QDir::homepath + "/file.exe");

It's the detached counterpart to QProcess::execute.

As of 5.15 that form is obsolete (but still present). The new preferred call is the same as the above but with a QStringList of command line arguments as the second parameter. If you don't have any arguments to pass just pass an empty list.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 1
    It's deprecated since 5.15. Use `QProcess::startDetached(const QString &program, const QStringList &arguments)` instead – Dmytro Nov 11 '21 at 13:57
  • @Dmytro confirmed in 5.15 docs and updated as best as I could from my phone. thanks!! – Jason C Nov 11 '21 at 14:59