在 Qt 中启动并写入终端
我正在使用 Qt 在 Linux 中编码。我知道使用 popen 或 QProcess 我可以从我的程序启动终端,但如何写入它?我用谷歌搜索了一下,人们建议使用 fork() 和 pipeline()。 我的目的是与终端进行 ICMP ping,并在 ping 成功时停止。我用 popen 做到了,但我无法停止 ping 过程,因此我的程序无法运行。
I am coding in linux using Qt. I understand that with popen or QProcess I can launch terminal from my program, but how do I write into to it? I google around people are suggesting fork() and pipe().
My purpose is to do an ICMP ping with the terminal, and stop when ping successfully. I made it with popen, but I couldn't stop the ping process thus my program won't run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会向终端写入任何内容,因为没有终端。您将要运行的程序的名称及其参数作为
QProcess 的参数传递::start
方法。如果您只需要知道 ping 是否成功,那么检查您之前使用 QProcess::start 启动的进程的退出代码就足够了;你不必阅读它的输出。来自 ping(8) - Linux 手册页
默认情况下,Linux 下的
ping
会一直运行,直到您停止为止。但是,您可以使用-c X
选项仅发送 X 个数据包,并使用-w X
选项将整个过程的超时设置为 X 秒。这样您就可以限制 ping 运行的时间。下面是使用
QProcess
在 Windows 上运行 ping 程序的工作示例。对于 Linux,您必须相应地更改 ping 选项(例如将-n
更改为-c
)。在示例中,ping 运行了 X 次,其中 X 是您为Ping
类构造函数提供的选项。一旦这些执行中的任何一个以退出代码 0(表示成功)返回,就会发出值为 true 的result
信号。如果没有执行成功,则发出result
信号,其值为 false。You don't write anything to terminal because there's no terminal. You pass name of a program to run and its arguments as arguments of the
QProcess::start
method. If you only need to know if ping was successful or not it's enough to check the exit code of the process which you started earlier usingQProcess::start
; you don't have to read its output.from ping(8) - Linux man page
By default
ping
under Linux runs until you stop it. You can however use-c X
option to send only X packets and-w X
option to set timeout of the whole process to X seconds. This way you can limit the time ping will take to run.Below is a working example of using
QProcess
to run ping program on Windows. For Linux you have to change ping options accordingly (for example-n
to-c
). In the example, ping is run up to X times, where X is the option you give toPing
class constructor. As soon as any of these executions returns with exit code 0 (meaning success) theresult
signal is emitted with value true. If no execution is successful theresult
signal is emitted with value false.