Results 1 to 10 of 10

Thread: Problem retrieving data from class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problem retrieving data from class

    I have a class called parse and i have another one called Taxi when i call out the data from the class parse class to the taxi class it shows me only 1 record from the map how would i be able to get the other records from it please see my code below and i will point out what im trying to do

    thanks..

    parse.h
    Qt Code:
    1. #ifndef PARSE_H
    2. #define PARSE_H
    3.  
    4. #include <QObject>
    5. #include <QtGui/QMainWindow>
    6. #include <QtGui/QScrollArea>
    7. #include <QtGui/QFrame>
    8. #include <QtGui/QVBoxLayout>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QLabel>
    11. #include <QtGui/QLineEdit>
    12. #include <QtGui/QGroupBox>
    13. #include <QtGui/QFormLayout>
    14. #include <QtGui/QMessageBox>
    15.  
    16. #include <QtCore/QPointer>
    17. #include <QtCore/QFile>
    18. #include <QtCore/QIODevice>
    19. #include <QtCore/QList>
    20. #include <QtCore/QMap>
    21. #include <QtCore/QString>
    22. #include "ui_taxi.h"
    23.  
    24. #include <QtXml/QXmlStreamReader>
    25.  
    26. class parse : public QObject
    27. {
    28. Q_OBJECT
    29. public:
    30. explicit parse(QObject *parent = 0);
    31. QStringListIterator *iterator;
    32.  
    33. //Member Methods
    34.  
    35.  
    36. //Accessor Functions
    37. QString getClientID() { return sClientID; }
    38.  
    39. signals:
    40.  
    41. public slots:
    42. void parseXML();
    43.  
    44.  
    45. private:
    46. QMap<QString, QString> parseClient(QXmlStreamReader& xml);
    47. void addElementDataToMap(QXmlStreamReader& xml, QMap<QString, QString>& map) const;
    48. void addClientsToUI(QList< QMap<QString,QString> >& clients);
    49. QString sClientID;
    50.  
    51. private slots:
    52.  
    53.  
    54. };
    55.  
    56. #endif // PARSE_H
    To copy to clipboard, switch view to plain text mode 

    parse.cpp
    Qt Code:
    1. #include "parse.h"
    2. #include "ui_taxi.h"
    3. #include <QtGui>
    4. #include <QtCore>
    5.  
    6.  
    7. parse::parse(QObject *parent) :
    8. QObject(parent)
    9. {
    10. }
    11.  
    12. void parse::parseXML() {
    13. /* We'll parse the xml file that was made from the sync application */
    14. QFile* file = new QFile("/ttm/myXML.xml");
    15. /* If we can't open it, let's show an error message. */
    16. if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
    17. // QMessageBox::critical(this,
    18. // "QXSRimpl::parseXML",
    19. // "Couldn't open strings.xml",
    20. // QMessageBox::Ok);
    21.  
    22. //If Cant open file
    23. qDebug() << "Couldn't Open /ttm/myXML.xml";
    24. return;
    25. }
    26. /* QXmlStreamReader takes any QIODevice. */
    27. QXmlStreamReader xml(file);
    28. QList< QMap<QString,QString> > clients;
    29. /* We'll parse the XML until we reach end of it.*/
    30. while(!xml.atEnd() &&
    31. !xml.hasError()) {
    32. /* Read next element.*/
    33. QXmlStreamReader::TokenType token = xml.readNext();
    34. /* If token is just StartDocument, we'll go to next.*/
    35. if(token == QXmlStreamReader::StartDocument) {
    36. continue;
    37. }
    38. /* If token is StartElement, we'll see if we can read it.*/
    39. if(token == QXmlStreamReader::StartElement) {
    40. /* If it's named Clients, we'll go to the next.*/
    41. if(xml.name() == "Clients") {
    42. continue;
    43. }
    44. /* If it's named Client, we'll dig the information from there.*/
    45. if(xml.name() == "Client") {
    46. clients.append(this->parseClient(xml));
    47. }
    48. }
    49. }
    50. /* Error handling. */
    51. if(xml.hasError()) {
    52. // QMessageBox::critical(this,
    53. // "parse::parseXML",
    54. // xml.errorString(),
    55. // QMessageBox::Ok);
    56.  
    57. //IF ERROR
    58. qDebug() << xml.errorString();
    59. }
    60. /* Removes any device() or data from the reader
    61.   * and resets its internal state to the initial state. */
    62. xml.clear();
    63. this->addClientsToUI(clients);
    64. }
    65.  
    66. QMap<QString, QString> parse::parseClient(QXmlStreamReader& xml) {
    67. QMap<QString, QString> client;
    68. /* Let's check that we're really getting a person. */
    69. if(xml.tokenType() != QXmlStreamReader::StartElement &&
    70. xml.name() == "Client") {
    71. return client;
    72. }
    73. /* Next element... */
    74. xml.readNext();
    75. /*
    76.   * We're going to loop over the things because the order might change.
    77.   * We'll continue the loop until we hit an EndElement named person.
    78.   */
    79. while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
    80. xml.name() == "Client")) {
    81. if(xml.tokenType() == QXmlStreamReader::StartElement) {
    82. /* We've found Client ID. */
    83. if(xml.name() == "Client_ID") {
    84. this->addElementDataToMap(xml, client);
    85. }
    86. }
    87. /* ...and next... */
    88. xml.readNext();
    89. }
    90. return client;
    91. }
    92.  
    93. void parse::addElementDataToMap(QXmlStreamReader& xml,
    94. QMap<QString, QString>& map) const {
    95. /* We need a start element, like <foo> */
    96. if(xml.tokenType() != QXmlStreamReader::StartElement) {
    97. return;
    98. }
    99. /* Let's read the name... */
    100. QString elementName = xml.name().toString();
    101. /* ...go to the next. */
    102. xml.readNext();
    103. /*
    104.   * This elements needs to contain Characters so we know it's
    105.   * actually data, if it's not we'll leave.
    106.   */
    107. if(xml.tokenType() != QXmlStreamReader::Characters) {
    108. return;
    109. }
    110. /* Now we can add it to the map.*/
    111. map.insert(elementName, xml.text().toString());
    112. }
    113.  
    114. void parse::addClientsToUI(QList< QMap<QString,QString> > &clients) {
    115. while(!clients.isEmpty()) {
    116.  
    117. QMap<QString,QString> client = clients.takeFirst();
    118.  
    119. sClientID = client["Client_ID"];
    120.  
    121. //Message box works shows both entries but when commented .........- Please see comment in taxi.cpp!!!!
    122. QMessageBox msgBox;
    123. msgBox.setText(sClientID);
    124. msgBox.exec();
    125. }
    126. }
    To copy to clipboard, switch view to plain text mode 

    taxi.h
    Qt Code:
    1. #ifndef TAXI_H
    2. #define TAXI_H
    3.  
    4. #include <QMainWindow>
    5. #include "parse.h"
    6.  
    7. namespace Ui {
    8. class taxi;
    9. }
    10.  
    11. class taxi : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit taxi(QWidget *parent = 0);
    17. ~taxi();
    18.  
    19. public slots:
    20.  
    21. private:
    22. Ui::taxi *ui;
    23. };
    24.  
    25. #endif // TAXI_H
    To copy to clipboard, switch view to plain text mode 

    taxi.cpp
    Qt Code:
    1. #include "taxi.h"
    2. #include "ui_taxi.h"
    3.  
    4. taxi::taxi(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::taxi)
    7. {
    8. ui->setupUi(this);
    9.  
    10. parse *_myParse = new parse();
    11. _myParse->parseXML();
    12.  
    13. QString sClientIdData;
    14. sClientIdData = _myParse->getClientID();
    15.  
    16. //CONTINUED FROM parse.cpp ......... This only returns 1 value how would i be able to get both values from it ? ... i know there is 2 values cause i can see it in the xml and see it from the other messagebox from parse.cpp
    17. QMessageBox msgBox;
    18. msgBox.setText(sClientIdData);
    19. msgBox.exec();
    20.  
    21. }
    22.  
    23. taxi::~taxi()
    24. {
    25. delete ui;
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by prophet0; 2nd March 2012 at 22:02.

Similar Threads

  1. Replies: 4
    Last Post: 7th April 2010, 23:09
  2. Data class concept
    By qtDave in forum General Programming
    Replies: 2
    Last Post: 19th December 2009, 09:41
  3. Many Data Memebers For a Class
    By MIH1406 in forum Qt Programming
    Replies: 2
    Last Post: 22nd September 2009, 20:00
  4. QT Class to decode PDU data
    By cutie.monkey in forum Newbie
    Replies: 0
    Last Post: 4th July 2009, 05:52
  5. default on class' public data
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 29th July 2008, 02:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.