QProcess::startDetached() 但隐藏控制台窗口 [英] QProcess::startDetached() but hide console window
问题描述
是否可以在隐藏控制台窗口的同时使用 QProcess::startDetached()
在 Windows 中运行程序?QProcess::start()
会隐藏控制台窗口,但 QProcess::startDetached()
不会,至少在默认情况下不会.
Is it possible to run a program in Windows using QProcess::startDetached()
while hiding its console window? QProcess::start()
will hide the console window but QProcess::startDetached()
does not, at least by default.
我可以使用 QProcess::start()
但是我必须等待子程序完成否则析构函数会强制终止它,我想立即关闭我的程序启动子进程后.
I could use QProcess::start()
but then I'd have to wait for the child program to finish or the destructor would force-terminate it, and I want to close my program immediately after starting the child process.
推荐答案
我遇到了完全相同的问题,无法以干净的方式解决它.我找到了 2 个选项:
I had exactly the same problem, and could not solve it in a clean way. I have found 2 options:
- Hacky 方法:子类
QProcess
并在启动进程后调用setProcessState(QProcess::NotRunning);
.这将阻止析构函数终止进程.但它依赖于QProcess
的实现细节 创建内存泄漏:在堆上动态创建
QProcess
,但永远不会删除它,因此永远不会调用其析构函数
- Hacky way: Subclass
QProcess
and callsetProcessState(QProcess::NotRunning);
after starting the process. This will prevent the destructor to terminate the process. But it relies on an implementation detail ofQProcess
Create a memory leak: Dynamically create the
QProcess
on the heap, but never delete it, and thus never invoke its destructor
{
QProcess *process = new QProcess;
process->start("taskkill", QStringList() << "/f" << "/im" << "My Service.exe");
}
这篇关于QProcess::startDetached() 但隐藏控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!