我已经用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毫秒= 30000)
  
  如果过程完成,则返回true;否则,返回false。否则返回false(如果
  操作超时,如果发生错误,或者此QProcess是
  已经完成了)。


因此,如果您的过程在30秒内完成,process->waitForFinished();将返回true,否则返回false。

如果您完全不希望超时,请使用process->waitForFinished(-1);

关于c++ - 在Qt和C++中从另一个应用程序启动一个应用程序会出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13821687/

10-11 23:07
查看更多