1

I have the following code which runs an exectuable using QProcess. The code all runs fine and the new executable runs and is all fine.

QString fileName = ui.textBrowser_csvFile->toPlainText();
QString tableName = ui.textBrowser_2->toPlainText();

QString program = "resources/myExe.exe";
QStringList arguments;
arguments << tableName << fileName;

bool res = QProcess::startDetached(program, arguments);

It is a Qt Console Application using QCoreApplication and there it doesn't spawn the terminal window like it would if I run it normally. It would be useful to monitor the progress of the executable so how do I get my Application to run the new program and display the terminal window?

Edit Possible duplicate does technically answer the question, but I have answered this question with a working solution.

GPPK
  • 6,546
  • 4
  • 32
  • 57
  • Does this help? https://stackoverflow.com/questions/42258892/qprocessstartdetached-not-show-console-window – Benp44 Aug 09 '17 at 15:25
  • Possible duplicate of [QProcess::startDetached() not show console window](https://stackoverflow.com/questions/42258892/qprocessstartdetached-not-show-console-window) – eyllanesc Aug 09 '17 at 15:31
  • Possible, however I think I have a better answer than that solution so I am going to post it below – GPPK Aug 09 '17 at 15:38

2 Answers2

1

So as discussed in the comments on my questions this StackOverflow post explains that this is infact correct behaviour when using the startDetached() function.

I'm not entirely sure what the answer to that question was suggesting to do but here is my working solution.

system() is a windows specific function which "can execute any command that can run on terminal if operating system allows" link

If I replace this line:

 bool res = QProcess::startDetached(program, arguments);

with the following, then it works:

system(QString("D:\\Qt\\5.9.1\\msvc2017_64\\bin\\myApp.exe " +tableName +" " + fileName).toStdString().c_str());

In the short term I have simply moved this application into the Qt folder because it needs the DLLs however with a proper release of this app you can run it from wherever, including from next to the application that is running it.

I do then get a terminal window and my app runns correctly.

GPPK
  • 6,546
  • 4
  • 32
  • 57
0

When migrating from Qt 5.7.0.0(x86) to 5.10.0.0(x64) I was really surprised to see that using the new Qt version, a child (launched with "QProcess::startDetached") process will not show up it's console (even though it's a console application! (SubSystem:CONSOLE))

MS documentation regarding "AllocConsole" says:

Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).

https://learn.microsoft.com/en-us/windows/console/allocconsole

Console processes are not attached to a console if they are created using CreateProcess with DETACHED_PROCESS

https://learn.microsoft.com/en-us/windows/console/creation-of-a-console

So I'm assuming that new Qt versions are using the "CreateProcess" with the "DETACHED_PROCESS" flag.

What I've ended up doing:

  • For child process I'm now using "SubSystem:WINDOWS". (Really important)
  • Inside the child process I'm creating the new console by using "AllocConsole()"
  • Using: "freopen("CONOUT$", "w", stderr);" and "freopen("CONOUT$", "w", stdout);" ("stderr" is really important if you want to capture qDebug, qInfo, etc...)

P.s.

If you would need to use "SubSystem:CONSOLE", be sure to call "FreeConsole" before calling "AllocConsole". This is required, because child process will by default be using parent process console...

Gediminas
  • 1,830
  • 3
  • 27
  • 47