PDA

View Full Version : Problem retrieving data from class



prophet0
2nd March 2012, 21:34
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


#ifndef PARSE_H
#define PARSE_H

#include <QObject>
#include <QtGui/QMainWindow>
#include <QtGui/QScrollArea>
#include <QtGui/QFrame>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QFormLayout>
#include <QtGui/QMessageBox>

#include <QtCore/QPointer>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include "ui_taxi.h"

#include <QtXml/QXmlStreamReader>

class parse : public QObject
{
Q_OBJECT
public:
explicit parse(QObject *parent = 0);
QStringListIterator *iterator;

//Member Methods


//Accessor Functions
QString getClientID() { return sClientID; }

signals:

public slots:
void parseXML();


private:
QMap<QString, QString> parseClient(QXmlStreamReader& xml);
void addElementDataToMap(QXmlStreamReader& xml, QMap<QString, QString>& map) const;
void addClientsToUI(QList< QMap<QString,QString> >& clients);
QString sClientID;

private slots:


};

#endif // PARSE_H


parse.cpp


#include "parse.h"
#include "ui_taxi.h"
#include <QtGui>
#include <QtCore>


parse::parse(QObject *parent) :
QObject(parent)
{
}

void parse::parseXML() {
/* We'll parse the xml file that was made from the sync application */
QFile* file = new QFile("/ttm/myXML.xml");
/* If we can't open it, let's show an error message. */
if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
// QMessageBox::critical(this,
// "QXSRimpl::parseXML",
// "Couldn't open strings.xml",
// QMessageBox::Ok);

//If Cant open file
qDebug() << "Couldn't Open /ttm/myXML.xml";
return;
}
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(file);
QList< QMap<QString,QString> > clients;
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() &&
!xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) {
continue;
}
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
/* If it's named Clients, we'll go to the next.*/
if(xml.name() == "Clients") {
continue;
}
/* If it's named Client, we'll dig the information from there.*/
if(xml.name() == "Client") {
clients.append(this->parseClient(xml));
}
}
}
/* Error handling. */
if(xml.hasError()) {
// QMessageBox::critical(this,
// "parse::parseXML",
// xml.errorString(),
// QMessageBox::Ok);

//IF ERROR
qDebug() << xml.errorString();
}
/* Removes any device() or data from the reader
* and resets its internal state to the initial state. */
xml.clear();
this->addClientsToUI(clients);
}

QMap<QString, QString> parse::parseClient(QXmlStreamReader& xml) {
QMap<QString, QString> client;
/* Let's check that we're really getting a person. */
if(xml.tokenType() != QXmlStreamReader::StartElement &&
xml.name() == "Client") {
return client;
}
/* Next element... */
xml.readNext();
/*
* We're going to loop over the things because the order might change.
* We'll continue the loop until we hit an EndElement named person.
*/
while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
xml.name() == "Client")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
/* We've found Client ID. */
if(xml.name() == "Client_ID") {
this->addElementDataToMap(xml, client);
}
}
/* ...and next... */
xml.readNext();
}
return client;
}

void parse::addElementDataToMap(QXmlStreamReader& xml,
QMap<QString, QString>& map) const {
/* We need a start element, like <foo> */
if(xml.tokenType() != QXmlStreamReader::StartElement) {
return;
}
/* Let's read the name... */
QString elementName = xml.name().toString();
/* ...go to the next. */
xml.readNext();
/*
* This elements needs to contain Characters so we know it's
* actually data, if it's not we'll leave.
*/
if(xml.tokenType() != QXmlStreamReader::Characters) {
return;
}
/* Now we can add it to the map.*/
map.insert(elementName, xml.text().toString());
}

void parse::addClientsToUI(QList< QMap<QString,QString> > &clients) {
while(!clients.isEmpty()) {

QMap<QString,QString> client = clients.takeFirst();

sClientID = client["Client_ID"];

//Message box works shows both entries but when commented .........- Please see comment in taxi.cpp!!!!
QMessageBox msgBox;
msgBox.setText(sClientID);
msgBox.exec();
}
}



taxi.h


#ifndef TAXI_H
#define TAXI_H

#include <QMainWindow>
#include "parse.h"

namespace Ui {
class taxi;
}

class taxi : public QMainWindow
{
Q_OBJECT

public:
explicit taxi(QWidget *parent = 0);
~taxi();

public slots:

private:
Ui::taxi *ui;
};

#endif // TAXI_H


taxi.cpp


#include "taxi.h"
#include "ui_taxi.h"

taxi::taxi(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::taxi)
{
ui->setupUi(this);

parse *_myParse = new parse();
_myParse->parseXML();

QString sClientIdData;
sClientIdData = _myParse->getClientID();

//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
QMessageBox msgBox;
msgBox.setText(sClientIdData);
msgBox.exec();

}

taxi::~taxi()
{
delete ui;
}

ChrisW67
3rd March 2012, 01:42
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().

prophet0
3rd March 2012, 19:55
would changing it to QStringList work?

ChrisW67
3rd March 2012, 23:30
Maybe, but that depends on your requirement; do you want one string containing all the ids joined together, or a list of ids?

prophet0
4th March 2012, 00:57
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 ?

prophet0
6th March 2012, 16:54
Any suggestions ? been on youtube and google for the past few days

ChrisW67
6th March 2012, 23:23
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.

prophet0
9th March 2012, 18:18
Well awesome its working so far :)



QStringList sClientIdData;
sClientIdData = _myParse->getClientID();

for ( QStringList::Iterator it = sClientIdData.begin(); it != sClientIdData.end(); ++it )
{
qDebug() << *it;
}


output



"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"3bddfb03c36b4221eee8ff59dd2f5d7e.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"
"fb0648859ad59e6c954a042376a054e4.avi"


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

ChrisW67
10th March 2012, 01:07
Single step through your code in your debugger. It will be far more informative than us guessing.

prophet0
11th March 2012, 08:14
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