1

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 application 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 solution.

EDIT:

I have alose 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;
//    });
    p.startDetached("cmd.exe",QStringList() << "/k" << "update.bat");
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • 1
    Try running your application outside of Qt-Creator. I don't use Qt-Creator with my Qt application development on windows and I get the console window. – drescherjm Jul 12 '21 at 12:44
  • I tried. When I press the button to trigger the above code, nothing happens. Should this have worked? – aarelovich Jul 12 '21 at 12:47
  • Maybe you need `cmd /k update.bat` – drescherjm Jul 12 '21 at 12:48
  • Tried it as well. Still nothing. – aarelovich Jul 12 '21 at 12:54
  • I forgot that I usually use QProcess in a GUI application to pull the output of a spawned console application and show it in a QTextBox or similar. Related: [https://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput](https://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput) – drescherjm Jul 12 '21 at 12:59
  • Ahh I see. However I actually need to the application to continue after the caller has finished. But thanks for the help. – aarelovich Jul 12 '21 at 13:04
  • There are a few questions on this topic: https://stackoverflow.com/questions/42258892/qprocessstartdetached-not-show-console-window https://stackoverflow.com/questions/45594248/qprocess-doesnt-show-the-command-window/48867208#48867208 – drescherjm Jul 12 '21 at 13:07
  • I have tried using sytem. It does not work for one major reason. Program execution is halted until the script has finished executing. And this is not the desired behaviour. – aarelovich Jul 12 '21 at 13:11
  • So I have been reading the question, but again, most people do not want their application to finish and the new one to just carry on. So the answers in those won't help. Or at least I don't see how. – aarelovich Jul 12 '21 at 13:13

1 Answers1

0

So this is not me figuring it out. This is more me coming up with a bypass.

Basically starting the script with system works. But the way I managed to make it work is like this:

  1. Call the system function from a separate thread. Otherwise the application locks to everything (including externally killing it) until whatever runs in the system is finished. You can use QThread for this.

  2. Kill the application as the first step of the script with taskkill: taskkill/im App.exe

This is not ideal for several things, mainly that I've seen in several places that calling system is not very safe. But since the script itself is generated by the application, in my case, it is good enough for my purposes.

aarelovich
  • 5,140
  • 11
  • 55
  • 106