2

I have a GUI program that start cli program by click button. I want to see console window and don't need wait until cli program end. So I use code like this:

QProcess::startDetached("cmd.exe");

After click button I don't see console window. But see cmd.exe process in task manager.

I tried use system() command but it freeze my app before cli program end.

Is there any way to make window visible?

Andrey
  • 23
  • 5

1 Answers1

0

It is the expected behavior. At least in Windows startDetached is equivalent to calling CreateProcess with the DETACHED_PROCESS flag, where the new process does not inherit its parent's console. It makes sense that in other platforms the method would do something similar.

In this case you'd had to manually allocate a new one using AllocConsole on the new process (be aware that you may need to redirect the streaming handles to the new console), or try to start the process in a different way (check CreateProcess or fork).

BTW, the reason system freezes your application is because it is a synchronous call, so it won't return the control until the other process finishes. You may try calling system from a separate thread and it this way you avoid blocking the main event loop of your application.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • Thank you for your answer. Now I am use QThread and system() command. This solution make code more complex, but this is work and cross-platform. – Andrey Feb 17 '17 at 21:05
  • BTW, if don't mind about using Boost in addition to Qt, and if the `system` call can be completely asynchronous, you can make it in a single line: `boost::thread([]() { system("cmd.exe"); });` – cbuchart Feb 17 '17 at 22:49