Question

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)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top