Qt Execute external program

こ雲淡風輕ζ 提交于 2019-11-28 07:24:08

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!