juangburgos / QConsoleListener

Small class to listen to console input asynchronously

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

QConsoleListener

Small Qt library to handle console input asynchronously using signals and slots. Based on gjorquera's snippet.

Usage

Add to project in *.pro file

include($$PWD/src/qconsolelistener.pri)

Include the header #include <QConsoleListener> and connect to the QConsoleListener::newLine signal. For example:

#include <QCoreApplication>
#include <QDebug>

#include <QConsoleListener>

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	// listen to console input
	QConsoleListener console;
	QObject::connect(&console, &QConsoleListener::newLine, 
	[&a](const QString &strNewLine) {
		qDebug() << "Echo :" << strNewLine;
		// quit
		if (strNewLine.compare("q", Qt::CaseInsensitive) == 0)
		{
			qDebug() << "Goodbye";
			a.quit();
		}
	});

	qDebug() << "Listening to console input:";
	return a.exec();
}

References

About

Small class to listen to console input asynchronously

License:MIT License


Languages

Language:C++ 50.3%Language:QMake 49.7%