using QTextStream to read stdin in a non-blocking fashion

后端 未结 2 1082
日久生厌
日久生厌 2021-01-04 21:37

Using Qt, I\'m attempting to read the contents of the stdin stream in a non-blocking fashion. I\'m using the QSocketNotifier to alert me when the socket has recieved some ne

相关标签:
2条回答
  • 2021-01-04 21:42

    I've found and example in other answer that fits almost to this question and with complete and simple code:

    https://stackoverflow.com/a/7389622/721929

    I've used it to implement a QT console based app with a textual menu to choose on user selection.

    0 讨论(0)
  • 2021-01-04 21:53

    Line buffering.

    Default is flushing after a "\n". If you write 5 lines to your process, your slot gets called 5 times. If you want to avoid that, you have to call setbuf(stdin, _IOFBF). But even then it is not guaranteed you can read arbitrarily large amounts of data in one chunk.

    Edit: It would probably better to use QTextStream::atEnd() instead of select, since QTextStream has its own internal buffers.

    0 讨论(0)
提交回复
热议问题