From: Gustavo Martin Morcuende Date: Fri, 6 Sep 2013 19:17:47 +0000 (+0200) Subject: Using QXmlStreamReader X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=refs%2Fremotes%2Forigin%2FHEAD;p=agent-device-independence%2F.git Using QXmlStreamReader --- diff --git a/Qt/DeviceAgent/DeviceAgent.pro.user b/Qt/DeviceAgent/DeviceAgent.pro.user index 374ab33..976b661 100644 --- a/Qt/DeviceAgent/DeviceAgent.pro.user +++ b/Qt/DeviceAgent/DeviceAgent.pro.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget @@ -54,7 +54,7 @@ Desktop Desktop Qt4ProjectManager.Target.DesktopTarget - 0 + 1 0 0 diff --git a/Qt/DeviceAgent/deviceagent.cpp b/Qt/DeviceAgent/deviceagent.cpp index 7d516ca..8ee2058 100644 --- a/Qt/DeviceAgent/deviceagent.cpp +++ b/Qt/DeviceAgent/deviceagent.cpp @@ -2,7 +2,7 @@ #include "ui_deviceagent.h" #include #include -#include +#include #include DeviceAgent::DeviceAgent(QWidget *parent) : @@ -12,9 +12,6 @@ DeviceAgent::DeviceAgent(QWidget *parent) : tcpServer = new QTcpServer(this); QHostAddress address = QHostAddress::LocalHost; tcpServer->listen(address, 3333); - file = new QTemporaryFile(); - file->open(); - out = new QTextStream (file); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection())); @@ -28,14 +25,15 @@ DeviceAgent::~DeviceAgent() void DeviceAgent::acceptConnection() { + QString string; clientConnection = tcpServer->nextPendingConnection(); + connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater())); connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), -//! [3] this, SLOT(displayError(QAbstractSocket::SocketError))); -// connect(clientConnection, SIGNAL(readyRead()), this, SLOT(dataFromClient())); + connect(clientConnection, SIGNAL(disconnected()), this, SLOT(endFromClient())); char data[10]; @@ -50,34 +48,51 @@ void DeviceAgent::acceptConnection() if (numData == -1) { clientConnection->close(); - endFromClient(); + endFromClient(string); return; } memcpy(currentData, data, numData); - - *out << currentData; + string.append(currentData); memset(data, 0, sizeof(data)); memset(currentData, 0, sizeof(currentData)); numData = clientConnection->read(data, sizeof(data)); } - - } else { clientConnection->close(); - endFromClient(); + endFromClient(string); return; } } } -void DeviceAgent::endFromClient() +void DeviceAgent::endFromClient(QString string) { clientConnection->deleteLater(); - file->close(); + QXmlStreamReader xml; + xml.addData(string); + + while (!xml.atEnd() && !xml.hasError()) + { + xml.readNext(); + if (xml.isStartElement()) + { + QString name = xml.name().toString(); + if (name == "rawData") + { + ui->lcdNumber->display(xml.readElementText()); + } + } + } + if (xml.hasError()) + { + } + else if (xml.atEnd()) + { + } } void DeviceAgent::displayError(QAbstractSocket::SocketError socketError) diff --git a/Qt/DeviceAgent/deviceagent.h b/Qt/DeviceAgent/deviceagent.h index f55c23e..725f577 100644 --- a/Qt/DeviceAgent/deviceagent.h +++ b/Qt/DeviceAgent/deviceagent.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include QT_BEGIN_NAMESPACE class QTcpServer; @@ -26,14 +26,12 @@ public: private slots: void acceptConnection(); void displayError(QAbstractSocket::SocketError socketError); - void endFromClient(); + void endFromClient(QString string); private: Ui::DeviceAgent *ui; QTcpServer *tcpServer; QTcpSocket *clientConnection; - QTextStream *out; - QTemporaryFile *file; }; #endif // DEVICEAGENT_H diff --git a/Qt/DeviceAgent/deviceagent.ui b/Qt/DeviceAgent/deviceagent.ui index c27d5ca..38bda3b 100644 --- a/Qt/DeviceAgent/deviceagent.ui +++ b/Qt/DeviceAgent/deviceagent.ui @@ -14,15 +14,37 @@ DeviceAgent - + - 70 - 60 - 241 + 30 + 30 + 301 51 + + 1 + + + 10 + + + QLCDNumber::Dec + + + QLCDNumber::Filled + + + + + + 40 + 120 + 291 + 91 + +