QProcess无法终止程序。

huangapple go评论116阅读模式
英文:

QProcess doesn't terminate the program

问题

QProcess pro;
pro.start("Notepad");
QTimer::singleShot(3000, &pro, &QProcess::terminate);

After 3 seconds, it should terminate my notepad.exe, but it's not working. I don't know what the problem is.

I tried changing it to (kill), but it still doesn't work.


<details>
<summary>英文:</summary>

QProcess pro;
pro.start("Notepad");
QTimer::singleShot(3000, &pro, &QProcess::terminate);


After 3 seconds it must terminate my notepad.exe but it won&#39;t I have no idea what&#39;s the problem

I changed it with (kill) but it still won&#39;t work

</details>


# 答案1
**得分**: 4

按设计(依赖于操作系统),在不使用完整路径运行命令相当于在POSIX中使用`system`函数,并且由于其实现方式,它对创建的进程没有这种粒度的控制,因为在您的应用程序和您打算执行的应用程序之间存在一个 shell 进程。由于`notepad.exe`是一个图形界面应用程序,它会立即返回控制权给 shell,之后 shell 被终止,这是程序存根代码的Windows特定行为。对于所有目的,您的`QProcess`已经停止。

在过去,Qt 在Windows上有特殊的行为,其中命令会传递给`cmd.exe`。有关运行、传递参数和终止进程的规则因程序而异,最显著的是`cmd.exe`本身。行为仍然会随着新的`startCommand`成员函数而变化。

您必须要么找到`notepad.exe`的真实位置,例如通过[`QStandardPaths::locate`][1](在Qt 5.0中引入)或要求用户配置它。


[1]: https://doc.qt.io/qt-6/qstandardpaths.html

<details>
<summary>英文:</summary>

By design (OS-dependant) running a command without full path is an equivalent of `system` function in POSIX and, by its implementation it doesn&#39;t have this granularity of control on created processes, because there was a shell process between your application and one you meant to execute. As `notepad.exe` is a GUI application, it immediately returns control to shell, which is terminated afterward, a Windows-specific behaviour of program&#39;s stub code. For all purposes your `QProcess` is already stopped.

Qt had in past special behaviour for Windows, where command are passed to `cmd.exe`. The rules regarding to running, passing arguments to and terminating processes change from program to program, most notable would be `cmd.exe` itself. Behaviour still vary with newfangled `startCommand` member function.

You have to either find the true location of notepad.exe, e.g. via [`QStandardPaths::locate`][1] (introduced in Qt 5.0) or ask user to configure it.


  [1]: https://doc.qt.io/qt-6/qstandardpaths.html

</details>



huangapple
  • 本文由 发表于 2023年8月10日 13:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定