[Interest] Non-blocking console reads

Jason H jhihn at gmx.com
Fri May 4 20:47:21 CEST 2018


I'm trying to (however inelegantly) treat the console as socket. I've tried using bytesAvailable() and readAll() but bytesAvailable() is always 0, and readAll will block. I do have an event loop and polling is ok with me. I'd rather not read the bytes in onTimeout(), and rather have the function connected to readSocket() do the reading.

Any ideas?

Console::Console(QObject *parent)
{
	if (!_in.open(stdin, QIODevice::ReadOnly|QFile::Unbuffered))
		qDebug() << "_in not ok";
	if (_out.open(stdout, QIODevice::WriteOnly))
		qDebug() << "out not ok";
	_buffer.open(QIODevice::ReadWrite);
	_timer.setSingleShot(false);
	_timer.start(100);

	connect(&_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
}
void Console::onTimeout() {
	int bytesAvail = _in.bytesAvailable();
	qDebug() << Q_FUNC_INFO << bytesAvail;
	if (bytesAvail > 0) {
		emit (readSocket(&_in));
	}
}



More information about the Interest mailing list