1

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

oh, but what about pathes? I mean how to set them in that case?
Ah, forgot to pass the executable name. Fixed.
oh, thanks, but I also meant full path to .exe or it suppose to be like this QStringLiteral("somepath/importdb_module")?
you can specify the full path in there, or modify the PATH via qt-project.org/doc/qt-5.0/qtcore/qprocessenvironment.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.