Run external program
-
Hi
Hook up error handler and see what it says.connect(process , &QProcess::errorOccurred, [=](QProcess::ProcessError error) { qDebug() << "error enum val = " << error << endl; });
-
@aminemaar said in Run external program:
my Qtquick application
but you're showing C++ code, am I missing something?
-
Hello! To start the external program, you can also check the
startDetached
(https://doc.qt.io/qt-5/qprocess.html#startDetached) andexecute
(https://doc.qt.io/qt-5/qprocess.html#execute) methods. You can even use the staticQProcess::startDetached("calc.exe");
method. -
@Cobra91151 The various detached methods won't help. Here @aminemaar needs to discover why its application is not running.
@aminemaar beside what @mrjj already wrote. What does your application do ? From the name it looks like some kind of network server ? In any case, did you also consider using the waitForStarted method ? This will allow you to know if the application started and in any case, check its status and potential error.
On a side note, doing it like that, you are leaking the QProcess object. How are you planning to handle it ?
-
Yes, I agree. So, how about this:
Code:
QProcess *appProcess = new QProcess(); connect(appProcess, &QProcess::started, [appProcess]() { qDebug() << "Started!"; qDebug() << "PID: " << appProcess->processId(); qDebug() << "Process state: " << appProcess->state(); }); connect(appProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [appProcess](int exitCode, QProcess::ExitStatus exitStatus) { qDebug() << exitCode; qDebug() << exitStatus; appProcess->deleteLater(); //clear the QProcess object }); appProcess->start("calc");
It will check the process state/pid, exit code and status and then delete the object using
deleteLater()
method.Also @aminemaar can try the native
Win API
function combined withQt
:HINSTANCE startApp(bool runAppAsAdmin, QString appPath, QString appArguments) { HINSTANCE hResult; if (runAppAsAdmin) { hResult = ShellExecute(nullptr, L"runas", appPath.toStdWString().c_str(), appArguments.toStdWString().c_str(), nullptr, SW_SHOW); } else { hResult = ShellExecute(nullptr, nullptr, appPath.toStdWString().c_str(), appArguments.toStdWString().c_str(), nullptr, SW_SHOW); } return hResult; }