我用 QtCreator 和 C++ 编写了一个 qt 快速桌面应用程序。
我想从我的应用程序启动另一个应用程序。我搜索并找到了这些选项:Qprocess,具有以下功能:start、startDetached 和 execute。
我要启动的应用,是一个单应用,别人建议我使用函数startDetached。
我选择了 startDetached 函数的这个选项:
bool startDetached ( const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0 )
这是我的代码:
QProcess *process=new QProcess(this);
bool res;
QStringList argsList;
argsList.append("-start");
process->startDetached(emulauncherInstallationDirectory + "\\Emulauncher.exe",argsList,emulauncherInstallationDirectory);
res = process->waitForFinished();
delete process;
process=NULL;
return res;
但是当我运行我的应用程序时,它有时运行良好,而在其他时候则根本无法运行。
调试了很多次,看到了函数
process->waitForFinished();
返回 false 或 true,没有明显的原因:.exe 文件一直在它的位置,如果我从命令行运行它,或者双击该文件,它运行良好,但从我的应用程序来看 - 它有时运行良好,有时则不然。
有人知道这个奇怪问题的任何原因或任何解决方案吗?
如有任何答复,我们将不胜感激。
最佳答案
来自文档:
bool QProcess::waitForFinished ( int msecs = 30000 )
Returns true if the process finished; otherwise returns false (if the operation timed out, if an error occurred, or if this QProcess is already finished).
因此,如果您的进程在 30 秒内完成,则 process->waitForFinished();
返回 true,否则返回 false。
如果您完全不想超时,请使用 process->waitForFinished(-1);
。
关于c++ - 在 Qt 和 C++ 中从另一个应用程序启动一个应用程序会出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821687/