Using QXmlStreamReader master origin/HEAD origin/master
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 6 Sep 2013 19:17:47 +0000 (21:17 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 6 Sep 2013 19:17:47 +0000 (21:17 +0200)
Qt/DeviceAgent/DeviceAgent.pro.user
Qt/DeviceAgent/deviceagent.cpp
Qt/DeviceAgent/deviceagent.h
Qt/DeviceAgent/deviceagent.ui

index 374ab33..976b661 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by Qt Creator 2.5.0, 2013-09-06T18:40:12. -->
+<!-- Written by Qt Creator 2.5.0, 2013-09-06T21:17:23. -->
 <qtcreator>
  <data>
   <variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -54,7 +54,7 @@
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Target.DesktopTarget</value>
-   <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
+   <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
    <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
    <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
    <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
index 7d516ca..8ee2058 100644 (file)
@@ -2,7 +2,7 @@
 #include "ui_deviceagent.h"
 #include <QtNetwork>
 #include <QtGui>
-#include <QTemporaryFile>
+#include <QXmlStreamReader>
 #include <string.h>
 
 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)
index f55c23e..725f577 100644 (file)
@@ -4,7 +4,7 @@
 #include <QMainWindow>
 #include <QTcpSocket>
 #include <QTextStream>
-#include <QTemporaryFile>
+#include <string.h>
 
 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
index c27d5ca..38bda3b 100644 (file)
    <string>DeviceAgent</string>
   </property>
   <widget class="QWidget" name="centralWidget">
-   <widget class="QTextEdit" name="textEdit">
+   <widget class="QLCDNumber" name="lcdNumber">
     <property name="geometry">
      <rect>
-      <x>70</x>
-      <y>60</y>
-      <width>241</width>
+      <x>30</x>
+      <y>30</y>
+      <width>301</width>
       <height>51</height>
      </rect>
     </property>
+    <property name="lineWidth">
+     <number>1</number>
+    </property>
+    <property name="numDigits">
+     <number>10</number>
+    </property>
+    <property name="mode">
+     <enum>QLCDNumber::Dec</enum>
+    </property>
+    <property name="segmentStyle">
+     <enum>QLCDNumber::Filled</enum>
+    </property>
+   </widget>
+   <widget class="QTextBrowser" name="textBrowser">
+    <property name="geometry">
+     <rect>
+      <x>40</x>
+      <y>120</y>
+      <width>291</width>
+      <height>91</height>
+     </rect>
+    </property>
    </widget>
   </widget>
   <widget class="QMenuBar" name="menuBar">