Results 1 to 17 of 17

Thread: Creating pointer to a Class

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating pointer to a Class

    I wrote this code:
    Qt Code:
    1. QString str = "http://www.forumcinemas.lv/rss/xml/movies/";
    2. Download_xml *Other = new Download_xml(str);
    3. QVector<Movie *> new_ml = Other->ml();
    To copy to clipboard, switch view to plain text mode 
    The problem i have found when going through the Debug:
    when new_ml is being assigned a value, Other->ml(); is empty.
    The thing is, that i thought when i write:
    Download_xml *Other = new Download_xml(str);
    new class would be automatically created but instead, it is created only after all those 3 lines of code. What's why when I assign the value to new_ml it is empty. How do i assign to new_ml the right value?

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    the problem must be method "ml()". please post that code...

    if "Other" was NULL, "Other->ml()" would create an access violation.

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    Here is Download_xml.h:
    Qt Code:
    1. class Download_xml : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. Download_xml(QString givenAddress, QWidget *parent = 0);
    6. ~Download_xml();
    7. QVector<Movie *> _ml;
    8.  
    9. public slots:
    10. void fetch(QString address);
    11. void finished(int id, bool error);
    12. void readData(const QHttpResponseHeader &);
    13.  
    14. QVector<Movie *> ml()
    15. {
    16. return _ml;
    17. }
    18.  
    19. private:
    20. void parseXml();
    21. QXmlStreamReader xml;
    22. QHttp http;
    23. int connectionId;
    24. // FOR XML
    25. QString currentTag;
    26. QString titleString;
    27. QString dateString;
    28. QString aboutString;
    29. QString pictureString;
    30.  
    31. #ifdef Q_OS_SYMBIAN
    32. // for bearer management
    33. QPointer<QNetworkSession> m_session;
    34. #endif
    35. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    please post your constructor code.

  5. #5
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include <QtNetwork>
    4. #include "download_xml.h"
    5. #include "movie.h"
    6.  
    7. Download_xml::Download_xml(QString givenAddress, QWidget *parent) : QWidget(parent)
    8. {
    9.  
    10. //OS_Symbian
    11. #ifdef Q_OS_SYMBIAN
    12. // Set Internet Access Point
    13. QNetworkConfigurationManager manager;
    14. const bool canStartIAP = manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces;
    15.  
    16. // Is there default access point, use it
    17. QNetworkConfiguration cfg = manager.defaultConfiguration();
    18. if (!cfg.isValid() || !canStartIAP)
    19. {
    20. // Available Access Points not found
    21. QMessageBox::warning(this, "Error", "No access point");
    22. return;
    23. }
    24.  
    25. m_session = new QNetworkSession(cfg);
    26. m_session->open();
    27. m_session->waitForOpened();
    28. #endif
    29.  
    30. connect(&http, SIGNAL(readyRead(QHttpResponseHeader)),
    31. this, SLOT(readData(QHttpResponseHeader)));
    32.  
    33. connect(&http, SIGNAL(requestFinished(int,bool)),
    34. this, SLOT(finished(int,bool)));
    35. //fetch(givenAddress);
    36. //fetch("http://rss.news.yahoo.com/rss/yahoonewsroom");
    37. //parseXml(givenAddress);
    38. }
    39.  
    40. void Download_xml::fetch(QString address)
    41. {
    42. xml.clear();
    43. QUrl url(address);
    44. http.setHost(url.host());
    45. connectionId = http.get(url.path());
    46. }
    47.  
    48. void Download_xml::readData(const QHttpResponseHeader &resp)
    49. {
    50. if (resp.statusCode() != 200)
    51. http.abort();
    52. else
    53. {
    54. xml.addData(http.readAll());
    55. }
    56. }
    57.  
    58. void Download_xml::finished(int id, bool error)
    59. {
    60. if (error)
    61. {
    62. QMessageBox::warning(this, "Error", http.errorString());
    63.  
    64. }
    65. else if (id == connectionId)
    66. {
    67. parseXml();
    68. }
    69.  
    70.  
    71. }
    72.  
    73. void Download_xml::parseXml()
    74. {
    75. bool movies = false;
    76. bool rss = false;
    77. bool movie = false;
    78. bool item = false;
    79. QVector<Movie *> movie_list;
    80.  
    81. while (!xml.atEnd() && movies == false && rss == false)
    82. {
    83. xml.readNext();
    84. if (xml.isStartElement())
    85. {
    86. currentTag = xml.name().toString();
    87. if (currentTag == "movieList")
    88. movies = true;
    89. if (currentTag == "rss")
    90. rss = true;
    91. }
    92. }
    93. while (!xml.atEnd() && movies == true)
    94. {
    95. xml.readNext();
    96. if (xml.isStartElement())
    97. {
    98. currentTag = xml.name().toString();
    99. if (currentTag == "movie")
    100. movie = true;
    101. }
    102. else if (xml.isEndElement() && movies == true && movie == true)
    103. {
    104. if (xml.name() == "movie")
    105. {
    106. Movie *one_movie = new Movie;
    107. one_movie->setName(titleString);
    108. one_movie->setDate(dateString);
    109. one_movie->setAbout(aboutString);
    110. one_movie->setPicture(pictureString);
    111. movie_list.append(one_movie);
    112. titleString.clear();
    113. dateString.clear();
    114. aboutString.clear();
    115. pictureString.clear();
    116. movie = false;
    117.  
    118. }
    119.  
    120. }
    121. else if (xml.isCharacters() && !xml.isWhitespace() && movies == true && movie == true)
    122. {
    123. if (currentTag == "title")
    124. titleString += xml.text().toString();
    125. else if (currentTag == "globalReleaseDate")
    126. dateString += xml.text().toString();
    127. else if (currentTag == "annotation")
    128. aboutString += xml.text().toString();
    129. else if (currentTag == "imageType1")
    130. pictureString += xml.text().toString();
    131. }
    132. }
    133.  
    134.  
    135.  
    136.  
    137. while (!xml.atEnd() && rss == true)
    138. {
    139. xml.readNext();
    140. if (xml.isStartElement())
    141. {
    142. currentTag = xml.name().toString();
    143. if (currentTag == "item")
    144. item = true;
    145. }
    146. else if (xml.isEndElement() && rss == true && item == true)
    147. {
    148. if (xml.name() == "item")
    149. {
    150. Movie *one_movie = new Movie;
    151. one_movie->setName(titleString);
    152. one_movie->setDate(dateString);
    153. one_movie->setAbout(aboutString);
    154. movie_list.append(one_movie);
    155. titleString.clear();
    156. dateString.clear();
    157. aboutString.clear();
    158. item = false;
    159. }
    160.  
    161. }
    162. else if (xml.isCharacters() && !xml.isWhitespace() && rss == true && item == true)
    163. {
    164. if (currentTag == "title")
    165. titleString += xml.text().toString();
    166. else if (currentTag == "pubDate")
    167. dateString += xml.text().toString();
    168. else if (currentTag == "description")
    169. aboutString += xml.text().toString();
    170. }
    171. }
    172.  
    173. if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError)
    174. {
    175. qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
    176. http.abort();
    177. }
    178.  
    179. _ml = movie_list;
    180.  
    181. }
    182.  
    183. Download_xml::~Download_xml()
    184. {
    185. }
    To copy to clipboard, switch view to plain text mode 

    Thing is that at the end of compilation, after those 3 lines a wrote above, the _ml fills up with data, but it's already to late, because the new_ml I want to use later is already been assigned an empty value.... I think it has to do something with QHttp or something...

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    in "parseXML", did you check if "movie_list" gets filled with correct values?

    why do you need "movie_list"? why don't you fill "_ml" directly?

  7. #7
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    about not filling _ml directly - i will think i will made _ml private argument.
    About parseXML - checking movie List is not the biggest problem - when the application is launching - this function does not even start, that's why _ml is empty when i copy it's content. But later the programm goes into this parseXML function, and movie_list does get filled with correct values.


    Added after 23 minutes:


    SO my question can be altered:
    How to make my download_xml class to complete the download, parsing the XML and fill the _ml, and then be able to copy it?
    Last edited by Archa4; 10th February 2011 at 11:30.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating pointer to a Class

    You are filling _ml in parseXml(), but you never call parseXml() in your constructor , so no wonder you _ml stays empty.(which is the wrong place to do what you are doing there any way, but that is another thread).
    Try:
    Qt Code:
    1. QString str = "http://www.forumcinemas.lv/rss/xml/movies/";
    2. Download_xml *Other = new Download_xml(str);
    3. Other->parseXml();
    4. QVector<Movie *> new_ml = Other->ml();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    I already tried that - the signals readyRead and requestFinished are not emitted, that's why nothing is happening. If i try Other->parseXml(); it wont work, cause at this moment the xml is empty. xml is filled when the function readData is called, and it's called when signal readyRead is emitted. So now my question is - how to make those signals appear?

    The thing is that parseXML(); is called after the signal requestFinished is recieved, but i have no idea how to make the program wait for those signals...

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating pointer to a Class

    Where do you call QHttp::request()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    Hm... Do I need to?
    Thing is, when this exact code was in the same file as those 3 lines i first wrote, and i wasn't using Object Movie, things were working. I was getting the contents of webpage, and was able to show them...

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating pointer to a Class

    Hm... Do I need to?
    Read the docs for when a readyRead() signal is emitted.

    Thing is, when this exact code was in the same file as those 3 lines i first wrote,
    What do you mean?
    your code is in a class....
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    your code is in a class....
    Now it is. But it was working before i moved it into seperate class

    The funny thing is - readyRead signal was emitting in the previous version, and i checked it again - there is no QHttp::request() in that code...
    Last edited by Archa4; 10th February 2011 at 12:22.

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating pointer to a Class

    Now it is. But it was working before i moved it into seperate class

    The funny thing is - readyRead signal was emitting in the previous version, and i checked it again - there is no QHttp::request() in that code...
    You probably have a problem with local stack vars getting out of scope... but I don't have the time look very carefully in to your code...

    When fetch() gets called?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #15
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    fetch gets called when the class Download_xml being initialized
    (last row)

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating pointer to a Class

    In your post it was commented out.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating pointer to a Class

    Yeah
    I tried to get it to work many times in different ways, for example i changed the constructor and did this:
    Qt Code:
    1. Download_xml *Nado = new Download_xml();
    2. QString str = "http://www.forumcinemas.lv/rss/xml/movies/";
    3. Nado->fetch(str);
    To copy to clipboard, switch view to plain text mode 
    But the signals in the Download_xml still are not generated. I simply cannot get it - why, when the whole thing is in one file, signals are generated, but when i moved it to different class Download_xml - those signals stopped working completely

Similar Threads

  1. Trouble with creating custom class
    By jstippey in forum Qt Programming
    Replies: 9
    Last Post: 12th January 2011, 15:08
  2. Replies: 4
    Last Post: 16th November 2008, 14:53
  3. pointer in a class
    By mickey in forum General Programming
    Replies: 23
    Last Post: 26th May 2008, 16:52
  4. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 16:59
  5. Question about pointer to class
    By kaydknight in forum General Programming
    Replies: 1
    Last Post: 17th January 2007, 08:15

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.