QProcess::startDetached() 但隐藏控制台窗口 [英] QProcess::startDetached() but hide console window

查看:190
本文介绍了QProcess::startDetached() 但隐藏控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在隐藏控制台窗口的同时使用 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:

  1. Hacky 方法:子类 QProcess 并在启动进程后调用 setProcessState(QProcess::NotRunning);.这将阻止析构函数终止进程.但它依赖于 QProcess
  2. 的实现细节
  3. 创建内存泄漏:在堆上动态创建QProcess,但永远不会删除它,因此永远不会调用其析构函数

  1. Hacky way: Subclass QProcess and call setProcessState(QProcess::NotRunning); after starting the process. This will prevent the destructor to terminate the process. But it relies on an implementation detail of QProcess
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆