Search code examples
c++windowsqtcommand-promptqt5.8

QProcess::startDetached() not show console window


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?


Solution

  • 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.