Results 1 to 10 of 10

Thread: Problem retrieving data from class

  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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem retrieving data from class

    addClientsToUI() iterates over your data structure and puts each client's ID into the same string. It overwrites, it doesn't append. The last client ID seen will be be the one returned by getClientID().

  3. #3
    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 Re: Problem retrieving data from class

    would changing it to QStringList work?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem retrieving data from class

    Maybe, but that depends on your requirement; do you want one string containing all the ids joined together, or a list of ids?

  5. #5
    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 Re: Problem retrieving data from class

    a list of ID's there will be others added to another list containing picture file names as strings so a list would be perfect

    so in the list i would like

    ID 1
    ID 2
    ID 3
    ID 4

    and another list

    file1.png
    file2.png
    file3.png
    file4.png

    EDIT

    I have a QMap why not use a QMapIterator but how would i iterate over in taxi.cpp ?
    Last edited by prophet0; 4th March 2012 at 01:58.

  6. #6
    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 Re: Problem retrieving data from class

    Any suggestions ? been on youtube and google for the past few days

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem retrieving data from class

    Your initial problem; use a QStringList as the return value of getClientID() and type of sClientID, and QStringList::append() or operator<<() at line 119 of the parse.cpp listing.

  8. #8
    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 Re: Problem retrieving data from class

    Well awesome its working so far

    Qt Code:
    1. QStringList sClientIdData;
    2. sClientIdData = _myParse->getClientID();
    3.  
    4. for ( QStringList::Iterator it = sClientIdData.begin(); it != sClientIdData.end(); ++it )
    5. {
    6. qDebug() << *it;
    7. }
    To copy to clipboard, switch view to plain text mode 

    output

    Qt Code:
    1. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    2. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    3. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    4. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    5. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    6. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    7. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    8. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    9. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    10. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    11. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    12. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    13. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    14. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    15. "3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
    16. "fb0648859ad59e6c954a042376a054e4.avi"
    17. "fb0648859ad59e6c954a042376a054e4.avi"
    18. "fb0648859ad59e6c954a042376a054e4.avi"
    19. "fb0648859ad59e6c954a042376a054e4.avi"
    20. "fb0648859ad59e6c954a042376a054e4.avi"
    21. "fb0648859ad59e6c954a042376a054e4.avi"
    22. "fb0648859ad59e6c954a042376a054e4.avi"
    23. "fb0648859ad59e6c954a042376a054e4.avi"
    24. "fb0648859ad59e6c954a042376a054e4.avi"
    25. "fb0648859ad59e6c954a042376a054e4.avi"
    26. "fb0648859ad59e6c954a042376a054e4.avi"
    27. "fb0648859ad59e6c954a042376a054e4.avi"
    28. "fb0648859ad59e6c954a042376a054e4.avi"
    29. "fb0648859ad59e6c954a042376a054e4.avi"
    30. "fb0648859ad59e6c954a042376a054e4.avi"
    31. "fb0648859ad59e6c954a042376a054e4.avi"
    32. "fb0648859ad59e6c954a042376a054e4.avi"
    33. "fb0648859ad59e6c954a042376a054e4.avi"
    To copy to clipboard, switch view to plain text mode 

    there are only 2 files but duplicated ? why is this doing that ?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem retrieving data from class

    Single step through your code in your debugger. It will be far more informative than us guessing.

  10. #10
    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 Re: Problem retrieving data from class

    I add a break point in line #4 of message #8 of this thread and the output for loop is making it add 33 items to the QStringlist

    going to try another way will report back what i am able to accomplish


    Added after 33 minutes:


    Well it was because i was using a while loop and it kept running and running so i think my problem is solved and learned a valuable lesson using loops :P thanks again
    Last edited by prophet0; 11th March 2012 at 08:14.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.