QProcess unknown error

☆樱花仙子☆ 提交于 2019-12-02 09:33:23

问题


I got strange problem. QProcess just not working!

And error is unknown.

I got global var in header

QProcess *importModule;

An I got this function ( I tried both start and startDetached methods btw )

    void App::openImport(){
      importModule = new QProcess();
      importModule->setWorkingDirectory(":\\Resources");
      importModule->startDetached("importdb_module.exe");
      QMessageBox::information(0,"",importModule->errorString());
}

It jsut outputs that error is unknown. Also it wont start other exes like

    void App::openImport(){
      importModule = new QProcess();
      importModule->setWorkingDirectory("C:\\Program Files\\TortoiseHg");
      importModule->startDetached("hg.exe");
      QMessageBox::information(0,"",importModule->errorString());
}

What I've done wrong? And is there other ways to run some .exe from my programm? Or maybe .bat files(which runs exes)? (Tried with QProcess too, not working)


回答1:


startDetached() is a static method and doesn't operate on importModule at all. It starts a process and then stops caring. Thus the error()/errorState() in importModule has nothing to do with the startDetached() call. What you want is start(). However, as QProcess is asynchronous, nothing will have happened yet immediately after start() returns. You must connect to the started(), error() and finished() signals to learn about the result.

connect(importModule, SIGNAL(started()), this, SLOT(importModuleStarted()));
connect(importModule, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(importModuleFinished(int, QProcess::ExitStatus)));
CONNECT(importModule, SIGNAL(error(QProcess::ProcessError)), this, SLOT(importModuleError(QProcess::ProcessError)));
importModule->start(QStringLiteral("importdb_module"), QStringList());

Alternatively you can use the blocking wait functions:

importModule->start(QStringLiteral("importdb_module"), QStringList());
importModule->waitForStarted(); // waits until starting is completed
importModule->waitForFinished(); // waits until the process is finished

However, I strongly advise against using them in the main thread, as they block the UI then.



来源:https://stackoverflow.com/questions/19873912/qprocess-unknown-error

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