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?

1
  • 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. Commented Oct 18, 2013 at 6:59

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Will the QProcess automatically deallocate itself, when the process has started?
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.
Is there another approach starting a process? I just want to fire a process and forget about it.
Yes, with the mentioned static method QProcess:startDetached
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. stackoverflow.com/questions/26670147/…
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.

1 Comment

This is an option worth considering. Execute 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.
5

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

QString file = QDir::homePath + QDir::separator + "file.exe";

1 Comment

On qt5.11 I had to use homePath() and separator().
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.

2 Comments

It's deprecated since 5.15. Use QProcess::startDetached(const QString &program, const QStringList &arguments) instead
@Dmytro confirmed in 5.15 docs and updated as best as I could from my phone. thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.