Qt Execute external program

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?

Answers


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

If you want your program to wait while the process is executing, you can use

QProcess::execute(file);

instead of

QProcess process;
process.start(file);

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

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

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.


Need Your Help

UDP Multicast over the internet?

c# udp multicast

I'm not sure how best to approach my problem. I have a service with runs on a remote machine with receives and process UDP packets. I want the service to be able to re-send these packets to anyone ...

symfony2: how to use group_concat in QueryBuilder

symfony doctrine-orm

I am having nested-set (using Gedmo tree) entity called "Location". Entity "Appartment" has location_id and what I need to do it to map scalar value called eg "path" to query that returns all appar...