#include <QTextStream>void write(const char* text="", const int enter=0);QTextStream output(stdout);QTextStream input(stdin);int main(){ char command[]=""; while (QString(command)!="exit"){ write("Scan code: "); input >> command; qDebug() << "Code = " << command; } return 0;}void write(const char* text, const int enter){ output << QString::fromUtf8(text); for (int i(0); i<=enter-1; i++){ output << "\n"; } output.flush();}
Scan code: 46106025Code = 46106025
#include <QTextStream>#include <QDebug>BarcodeReader::BarcodeReader(QObject *parent) : QObject(parent), notifier(0, QSocketNotifier::Read){ connect(¬ifier, SIGNAL(activated(int)), this, SLOT(text()));}void BarcodeReader::text(){ QTextStream qin(stdin); QString line = qin.readLine(); qDebug() << "BarcodeReader::text = " << line; emit textReceived(line);}
#include <QObject>#include <QSocketNotifier>class BarcodeReader : public QObject{ Q_OBJECTpublic: explicit BarcodeReader(QObject *parent = 0);signals: void textReceived(QString message);public slots: void text();private: QSocketNotifier notifier;};
class KeyPressEater:public QObject{ Q_OBJECTprotected: bool eventFilter(QObject *obj, QEvent *event){ if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("Ate key press %d", keyEvent->key()); if(keyEvent->key()==Qt::Key_Enter||keyEvent->key()==Qt::Key_Return){ barCode=tempCode; tempCode.clear(); qDebug()<<barCode; QString tmp="Штрих код был успешно считан устройством. Номер:"+barCode; QMessageBox::information(NULL,"Bar code",tmp); return true; } tempCode+=keyEvent->key(); return true; } else { return QObject::eventFilter(obj, event); } }private: QString tempCode,barCode;};