Qt World Summit: Register Today!

Run external program

  • Hi,
    I want to run external application in my Qtquick application,
    I used this code but it's not working

    QProcess*   process = new QProcess();
    QString program = "C:\\WTB\\Serveur\\svr.exe";

  • Lifetime Qt Champion

    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) and execute (https://doc.qt.io/qt-5/qprocess.html#execute) methods. You can even use the static QProcess::startDetached("calc.exe"); method.

  • Lifetime Qt Champion

    @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 ?

  • @SGaist

    Yes, I agree. So, how about this:


    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

    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 with Qt:

    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;

Log in to reply