QProcess :: startDetached()不显示控制台窗口 [英] QProcess::startDetached() not show console window
问题描述
我有一个GUI程序,可通过单击按钮启动cli程序。我想查看控制台窗口,不需要等到cli程序结束。所以我使用这样的代码:
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");
单击按钮后,我看不到控制台窗口。但是请参阅任务管理器中的cmd.exe进程。
After click button I don't see console window. But see cmd.exe process in task manager.
我尝试使用system()命令,但它会在cli程序结束前冻结我的应用程序。
I tried use system() command but it freeze my app before cli program end.
有什么方法可以使窗口可见?
Is there any way to make window visible?
推荐答案
这是预期的行为。至少在Windows中, startDetached
等效于使用 DETACHED_PROCESS
标志,其中新进程不会继承其父级的控制台。在其他平台上,该方法也可以执行类似的操作。
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.
在这种情况下,您必须使用 AllocConsole
上的新进程(请注意,您可能需要将流句柄重定向到新控制台),或尝试以其他方式启动该进程(检查 CreateProcess
或 叉子
)。
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,系统
冻结应用程序的原因是因为它是一个同步调用,所以它在其他过程完成之前,不会返回控件。您可以尝试从单独的线程调用 system
,这样可以避免阻塞应用程序的主事件循环。
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.
这篇关于QProcess :: startDetached()不显示控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!