Results 1 to 3 of 3

Thread: Calling class functions from another function

  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 Calling class functions from another function

    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. parse *myParse;
    24. };
    25.  
    26. #endif // TAXI_H
    To copy to clipboard, switch view to plain text mode 

    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.  
    32. private slots:
    33.  
    34. signals:
    35.  
    36. public slots:
    37. void parseXML();
    38.  
    39. private:
    40. QPointer<QVBoxLayout> _layout;
    41.  
    42. QMap<QString, QString> parseClient(QXmlStreamReader& xml);
    43. void addElementDataToMap(QXmlStreamReader& xml,
    44. QMap<QString, QString>& map) const;
    45.  
    46. void addClientsToUI(QList< QMap<QString,QString> >& clients);
    47.  
    48. };
    49.  
    50. #endif // PARSE_H
    To copy to clipboard, switch view to plain text mode 

    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. // /* Let's get the attributes for person */
    74. // QXmlStreamAttributes attributes = xml.attributes();
    75. // /* Let's check that person has id attribute. */
    76. // if(attributes.hasAttribute("Client_ID")) {
    77. // /* We'll add it to the map. */
    78. // client["Client_ID"] = attributes.value("Client_ID").toString();
    79. // }
    80. /* Next element... */
    81. xml.readNext();
    82. /*
    83.   * We're going to loop over the things because the order might change.
    84.   * We'll continue the loop until we hit an EndElement named person.
    85.   */
    86. while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
    87. xml.name() == "Client")) {
    88. if(xml.tokenType() == QXmlStreamReader::StartElement) {
    89. /* We've found Client ID. */
    90. if(xml.name() == "Client_ID") {
    91. this->addElementDataToMap(xml, client);
    92. }
    93. /* We've found Network ID. */
    94. if(xml.name() == "Network_ID") {
    95. this->addElementDataToMap(xml, client);
    96. }
    97. /* We've found Video. */
    98. if(xml.name() == "Video") {
    99. this->addElementDataToMap(xml, client);
    100. }
    101. /* We've found Description. */
    102. if(xml.name() == "Description") {
    103. this->addElementDataToMap(xml, client);
    104. }
    105. /* We've found Zone6. */
    106. if(xml.name() == "Zone6") {
    107. this->addElementDataToMap(xml, client);
    108. }
    109. /* We've found status. */
    110. if(xml.name() == "status") {
    111. this->addElementDataToMap(xml, client);
    112. }
    113. }
    114. /* ...and next... */
    115. xml.readNext();
    116. }
    117. return client;
    118. }
    119.  
    120. void parse::addElementDataToMap(QXmlStreamReader& xml,
    121. QMap<QString, QString>& map) const {
    122. /* We need a start element, like <foo> */
    123. if(xml.tokenType() != QXmlStreamReader::StartElement) {
    124. return;
    125. }
    126. /* Let's read the name... */
    127. QString elementName = xml.name().toString();
    128. /* ...go to the next. */
    129. xml.readNext();
    130. /*
    131.   * This elements needs to contain Characters so we know it's
    132.   * actually data, if it's not we'll leave.
    133.   */
    134. if(xml.tokenType() != QXmlStreamReader::Characters) {
    135. return;
    136. }
    137. /* Now we can add it to the map.*/
    138. map.insert(elementName, xml.text().toString());
    139. }
    140.  
    141. void parse::addClientsToUI(QList< QMap<QString,QString> >& clients) {
    142. while(!clients.isEmpty()) {
    143. QGroupBox* clientGB = new QGroupBox("Client");
    144. QFormLayout* layout = new QFormLayout;
    145. QMap<QString,QString> client = clients.takeFirst();
    146. layout->addRow("Client ID", new QLineEdit(client["Client_ID"]));
    147. layout->addRow("Network ID", new QLineEdit(client["Network_ID"]));
    148. layout->addRow("Video", new QLineEdit(client["Video"]));
    149. layout->addRow("Description", new QLineEdit(client["Description"]));
    150. layout->addRow("Zone6", new QLineEdit(client["Zone6"]));
    151. layout->addRow("status", new QLineEdit(client["status"]));
    152. clientGB->setLayout(layout);
    153. this->_layout->addWidget(clientGB);
    154. }
    155. }
    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. connect(ui->pushButton, SIGNAL(clicked()), myParse, SLOT(parseXML()));
    11. }
    12.  
    13. taxi::~taxi()
    14. {
    15. delete ui;
    16. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Calling class functions from another function

    This is a C++ issue, not a Qt one. Moving to general programming.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  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: Calling class functions from another function

    This post was and error on my C++ skills quick google search shed light on my issue

    thanks

Similar Threads

  1. Replies: 7
    Last Post: 2nd September 2010, 19:42
  2. class calling other class functions?
    By Hardstyle in forum Newbie
    Replies: 4
    Last Post: 2nd June 2010, 02:38
  3. QtConncurrent - calling function within the class
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2009, 17:37
  4. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 21:32
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26

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.