Qt Execute external program

2019-01-23 03:06发布

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?

标签: c++ qt external
3条回答
来,给爷笑一个
2楼-- · 2019-01-23 03:23

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);
查看更多
Evening l夕情丶
3楼-- · 2019-01-23 03:23

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

QString file = QDir::homePath + QDir::separator + "file.exe";
查看更多
狗以群分
4楼-- · 2019-01-23 03:28

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);
查看更多
登录 后发表回答