Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I start an application that will live on after the caller app has finished?



  • This is what I wanted to do.

    I have a script that currently just starts prints a message. Waits 5 seconds and prints another message before finishing by asking the user to press any key to end.

    And I have a Qt application that needs to call this script and then quit. I want the console window to open up to display and the script text and this new process to be able to continue after the Qt appliction has finished.

    In windows my script is called "update.bat"

    So I have tried:

    QProcess::startDetached("update.bat");
    QCoreApplication::quit();
    but the console (windows one) does NOT open up (all output is in QtCreator) and it definitely does not continue after the application is finished.

    Is there a way to do this using Qt?

    I don't want to rely on the CreateProcess from windows, if possible, and use Qt soloution.

    I have also tried the setCreateProcessArgumentModifier and to start the update.bat script with cmd /k. The results for all of this is exactly the same. Output is redirected to the QtCreator console and when executing the application outside of Qt nothing happens (this is all happens when you press a button).

    QProcess p;
    //    QProcess process;
    //    process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments *args)
    //    {
    //        args->flags |= CREATE_NEW_CONSOLE;
    //        args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
    //        args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
    //        args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
    //                                           | FOREGROUND_INTENSITY;
    //    });
    // process.startDetached("cmd.exe",QStringList() << "/k" << "update.bat");
        p.startDetached("cmd.exe",QStringList() << "/k" << "update.bat");
    

    Finally I have tried using system. Like this:

    system(QString("update.bat").toStdString().c_str());
    

    The problem with this is that it's blocking. So I can't quit the application. And If I try to terminate it from the script it doesn't die until the script is done.

    Any other suggestions?



  • @aarelovich said in How can I start an application that will live on after the caller app has finished?:

    p.startDetached("cmd.exe",QStringList() << "/k" << "update.bat");

    To start a detached process with Windows system, you could try this:
    p.start("cmd.exe", QStringList() << "/c" << "start" << "update.bat"),



  • @KroMignon said in How can I start an application that will live on after the caller app has finished?:

    p.start("cmd.exe", QStringList() << "/c" << "start" << "update.bat"),

    When I tried the Qt Creator console (no reaction when exectuting stand alone) printed this:

    QProcess: Destroyed while process ("cmd.exe") is still running.
    

    I actually found a work around for this which it spawn a separate thread then use system to call the script and finally have the script itself kill the application.

    For what I want that's good enough. Thank you anyways.


Log in to reply