如何获得类似QIODevice的信号用于控制台输入(stdin)? [英] How to get QIODevice-like signals for console input (stdin)?
问题描述
我正在编写基于 QCoreApplication
的控制台应用程序,我想得到信号,当用户向stdin中输入某些内容时(一次或按字符输入整行)对我的应用而言并不重要)。
I'm writing console app, based QCoreApplication
, and I want to get signals, when user input something into stdin (whole line at once or by characters, it is not important for my app).
QFile
不会根据文档发出信号,因此任何有关如何获得信号的想法如果用户将内容放入 stdin
?
QFile
not emit signals according to documentation, so any ideas how to get signal if user put in something to stdin
?
现在我正在使用qt 4.8.7 / linux / amd64,但我希望将来将此代码移植到macosx和Windows。
At now I'm working with qt 4.8.7/linux/amd64, but I want port this code to macosx and windows in the future.
更新
我发现了可以在基于* nix的 OS
上运行的解决方案,例如 linux
和 mac os x
:
I found solution that should work on *nix based OS
, like linux
and mac os x
:
https://gist.github.com/gjorquera/2576569
但是关于Windows,也可以吗? QSocketNotfier
?
But what about windows, would it also with with QSocketNotfier
?
推荐答案
对于Unix(Linux,OS X),此答案有一个解决方案。
For Unix (Linux, OS X), this answer has a solution.
对于Windows,您不能使用 QSocketNotifier
:而是在从 GetStdHandle
获取的控制台句柄上使用 QWinEventNotifier
(请参阅此答案)。该代码与上面答案中的Unix变体几乎相同。
For Windows, you cannot use QSocketNotifier
: instead, use a QWinEventNotifier
on the console handle you obtain from GetStdHandle
(see this answer). The code will be almost identical to the Unix variant from the answer above.
这篇关于如何获得类似QIODevice的信号用于控制台输入(stdin)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!