我想从我的QT-Programm开始一个外部程序.唯一可行的解决方案是:
system("start explorer.exe");
Run Code Online (Sandbox Code Playgroud)
但它只适用于Windows并且暂时启动命令行.
我试过的下一件事是:
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
//process.execute(file); //i tried as well
Run Code Online (Sandbox Code Playgroud)
但什么都没发生.有任何想法吗?
tom*_*odi 29
如果您的process对象是堆栈上的变量(例如,在方法中),则代码将无法按预期工作,因为QProcess当方法完成时,您已经启动的进程将在析构函数中被终止.
void MyClass::myMethod()
{
QProcess process;
QString file = QDir::homepath + "file.exe";
process.start(file);
}
Run Code Online (Sandbox Code Playgroud)
你应该改为QProcess在堆上分配对象:
QProcess *process = new QProcess(this);
QString file = QDir::homepath + "/file.exe";
process->start(file);
Run Code Online (Sandbox Code Playgroud)
如果您希望程序在进程执行时等待,则可以使用
QProcess::execute(file);
Run Code Online (Sandbox Code Playgroud)
代替
QProcess process;
process.start(file);
Run Code Online (Sandbox Code Playgroud)
QDir :: homePath不以分隔符结尾.exe的有效路径
QString file = QDir::homePath + QDir::separator + "file.exe";
Run Code Online (Sandbox Code Playgroud)
只需使用QProcess::startDetached; 它是静态的,你不需要担心等待它完成或在堆上分配东西或类似的事情:
QProcess::startDetached(QDir::homepath + "/file.exe");
Run Code Online (Sandbox Code Playgroud)
它是 的独立对应物QProcess::execute。
从 5.15 开始,该表单已过时(但仍然存在)。新的首选调用与上面的相同,但使用QStringList命令行参数作为第二个参数。如果您没有任何参数可传递,则只需传递一个空列表。