QProcess未知错误 [英] QProcess unknown error

查看:75
本文介绍了QProcess未知错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题.QProcess不能正常工作!

I got strange problem. QProcess just not working!

错误是未知的.

我在标头中有全局变量

QProcess *importModule;

我有这个功能(我尝试了 start startDetached 方法btw)

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());
}

它会提示错误未知.而且它也不会启动其他exe

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());
}

我做错了什么?还有其他方法可以从我的程序中运行某些 .exe 吗?还是 .bat 文件(运行exes)?(也尝试过QProcess,不起作用)

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)

推荐答案

startDetached()是静态方法,根本不对 importModule 进行操作.它开始一个过程,然后停止护理.因此, importModule 中的error()/errorState()与 startDetached()调用无关.您想要的是 start().但是,由于QProcess是异步的,因此start()返回后立即不会发生任何事情.您必须连接到 started() error() finished()信号以了解结果.

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

但是,我强烈建议不要在主线程中使用它们,因为它们会阻塞UI.

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

这篇关于QProcess未知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆