PDA

View Full Version : Calling class functions from another function



prophet0
29th February 2012, 15:38
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;
parse *myParse;
};

#endif // TAXI_H


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);

private slots:

signals:

public slots:
void parseXML();

private:
QPointer<QVBoxLayout> _layout;

QMap<QString, QString> parseClient(QXmlStreamReader& xml);
void addElementDataToMap(QXmlStreamReader& xml,
QMap<QString, QString>& map) const;

void addClientsToUI(QList< QMap<QString,QString> >& clients);

};

#endif // PARSE_H




#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;
}
// /* Let's get the attributes for person */
// QXmlStreamAttributes attributes = xml.attributes();
// /* Let's check that person has id attribute. */
// if(attributes.hasAttribute("Client_ID")) {
// /* We'll add it to the map. */
// client["Client_ID"] = attributes.value("Client_ID").toString();
// }
/* 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);
}
/* We've found Network ID. */
if(xml.name() == "Network_ID") {
this->addElementDataToMap(xml, client);
}
/* We've found Video. */
if(xml.name() == "Video") {
this->addElementDataToMap(xml, client);
}
/* We've found Description. */
if(xml.name() == "Description") {
this->addElementDataToMap(xml, client);
}
/* We've found Zone6. */
if(xml.name() == "Zone6") {
this->addElementDataToMap(xml, client);
}
/* We've found status. */
if(xml.name() == "status") {
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()) {
QGroupBox* clientGB = new QGroupBox("Client");
QFormLayout* layout = new QFormLayout;
QMap<QString,QString> client = clients.takeFirst();
layout->addRow("Client ID", new QLineEdit(client["Client_ID"]));
layout->addRow("Network ID", new QLineEdit(client["Network_ID"]));
layout->addRow("Video", new QLineEdit(client["Video"]));
layout->addRow("Description", new QLineEdit(client["Description"]));
layout->addRow("Zone6", new QLineEdit(client["Zone6"]));
layout->addRow("status", new QLineEdit(client["status"]));
clientGB->setLayout(layout);
this->_layout->addWidget(clientGB);
}
}


taxi.cpp


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

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

connect(ui->pushButton, SIGNAL(clicked()), myParse, SLOT(parseXML()));
}

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



im trying to call the parseXML but i get no error just The program has unexpectedly finished. i try to debug but saying

error is
connect(ui->pushButton, SIGNAL(clicked()), myParse, SLOT(parseXML()));

my question is this how can i call parseXML from other classes and retrieve the data contained in the map?

thanks

wysota
29th February 2012, 16:36
This is a C++ issue, not a Qt one. Moving to general programming.

prophet0
2nd March 2012, 21:16
This post was and error on my C++ skills quick google search shed light on my issue

thanks