Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==================== main.cpp
- #include <QtCore/QCoreApplication>
- #include "stdinstreamreader.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- StdInStreamReader reader;
- return a.exec();
- }
- //==================== stdinstreamreader.h
- #ifndef STDINSTREAMREADER_H
- #define STDINSTREAMREADER_H
- #include <QObject>
- #include <QSocketNotifier>
- class StdInStreamReader : public QObject
- {
- Q_OBJECT
- public:
- explicit StdInStreamReader(QObject *parent = 0);
- signals:
- public slots:
- void receivedData(const int &socketId);
- };
- #endif // STDINSTREAMREADER_H
- //==================== stdinstreamreader.cpp
- #include "stdinstreamreader.h"
- #include <QDebug>
- #include <QCoreApplication>
- StdInStreamReader::StdInStreamReader(QObject *parent) :
- QObject(parent)
- {
- QSocketNotifier *notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read, this);
- connect(notifier, SIGNAL(activated(int)), this, SLOT(receivedData(int)));
- notifier->setEnabled(true);
- }
- void StdInStreamReader::receivedData(const int &socketId)
- {
- Q_UNUSED(socketId)
- QByteArray ba;
- bool close = false;
- while (true) {
- char c = getc(stdin);
- if (c == EOF) {
- close = true;
- }
- if (c == EOF || c == '\0' || c == '\n') {
- break;
- }
- if (c)
- ba.append(c);
- }
- qDebug() << "Received: " << QString::fromLocal8Bit(ba);
- if (QString::fromLocal8Bit(ba).startsWith("close") || close) {
- QCoreApplication::exit(EXIT_SUCCESS);
- }
- }
Add Comment
Please, Sign In to add comment