using QTextStream to read stdin in a non-blocking fashion

后端 未结 2 1099
日久生厌
日久生厌 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: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.

提交回复
热议问题