Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Problem showing the right thing.

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

    Default Problem showing the right thing.

    So i had a problem showing the right data after parsing xml. I found a way, but now i need help from u guys:
    My main.cpp calls for list.cpp (here is the important part):
    Qt Code:
    1. List::List(bool first, QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. if(first == true)
    5. {
    6. QString str = "http://www.forumcinemas.lv/rss/xml/movies/";
    7. Download_xml *Other= new Download_xml(str);
    8. this->hide();
    9. }
    10. else
    11. {
    12. layout = new QVBoxLayout(this);
    13. listWidget = new QListWidget(this);
    14. layout->addWidget(listWidget);
    15. this->setFocus();
    16. }
    To copy to clipboard, switch view to plain text mode 

    As u can see, i call for download_xml, where the xml parsing will take place.
    Here is download_xml.cpp part.
    Qt Code:
    1. void Download_xml::parseXml()
    2. {
    3. QVector<Movie *> movie_list;
    4. List *list = new List(false);
    5.  
    6. ...
    7.  
    8. _ml = movie_list;
    9. list->completeList(_ml);
    10. }
    To copy to clipboard, switch view to plain text mode 

    and here is the list.cpp function part:
    Qt Code:
    1. void List::completeList(QVector<Movie *> ml)
    2. {
    3.  
    4. QVector<Movie *> new_ml = ml;
    5. for (int i = 0; i<new_ml.size(); i++)
    6. {
    7. listWidget->insertItem(i, "");
    8. listWidget->setItemWidget(listWidget->item(i), new Custom_Widget
    9. (new_ml[i]->name(), new_ml[i]->date(), new_ml[i]->about(), new_ml[i]->picture()));
    10. listWidget->item(i)->setSizeHint(QSize (350,470));
    11. listWidget->setSpacing(1);
    12. }
    To copy to clipboard, switch view to plain text mode 

    The problem is, i can't see the list that is created after the parsing in XML takes place. I run through the debug, and i think that the list is created and even shown, but in background. In front all u can see is the first list, that is created empty.
    That's why I ask your help: either to show the list, that is created after parsing in front, or help me rewrite the code in some way it would work better...

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

    Default Re: Problem showing the right thing.

    What is the point of copying the vector here and there? And why are you hiding your widget in the first code snippet you posted?
    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
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem showing the right thing.

    That was one of the things i tried to hide the first list created...

  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: Problem showing the right thing.

    What is the reason for "QVector<Movie *> new_ml = ml;" in "completeList()"?

    I'm not sure, if I understand your code. First, you create a "List" with parameter "first==true", then the xml parsing is done. During this parsing, in "parseXml()" a new "List" is created with "first==false". Then you call "completeList()", where you display the elements of the parameter "ml", which is "movie_list" which is "_ml" from the first list. Is that correct?

    Why do you create several "List"-objects?

  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: Problem showing the right thing.

    You are almost correct:
    "you display the elements of the parameter "ml", which is "movie_list" which is "_ml" from the first list"
    , they are not from the first list, but from download_xml.
    First list does not contain any items. It's empty

    About why did i do that - I couldn't find other way around it. If you can suggest any other way (for example, start with download_xml, and then show the list), i would really appreciate it

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

    Default Re: Problem showing the right thing.

    Why don't you just populate the list you have instead of creating a new one?
    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.


  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: Problem showing the right thing.

    That was the first thing i tried. Problem is - i cannot call the mexthod completeList from Download_xml, without an object. And I have no idea how to call this method for the list that has already been created... Can u help me with code?

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

    Default Re: Problem showing the right thing.

    Just return a list of QListWidgetItem objects and then put them into the list when you have access to the list.
    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.


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

    Default Re: Problem showing the right thing.

    what about something like this (untested):

    Qt Code:
    1. void function()
    2. {
    3.  
    4. QVector<Movie*> vec = ParseXml("http://www.forumcinemas.lv/rss/xml/movies/");
    5. QListWidget* listWidget = new QListWidget();
    6. FillListWidget(listWidget, vec);
    7.  
    8. listWidget->show();
    9. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Problem showing the right thing.

    See here is a problem: i cannot return this list into constructor. The reason - i have to end the List constructor for parsingXML to finish. But if I end the constructor,then I have no idea how to return the list from Download_xml to the List class...

    what about something like this
    I cannot call ParseXml, because in the Download_xml there are 2 signals that have to appear before parseXML is called. If i just try ParseXml, it will parse an empty XML, that has not been filled, cause the ReadyRead signal has not appeared yet...


    Added after 10 minutes:


    You are NEVER gonna believe this - i made it work
    I made a layout in the first list that was created and added the newly created Download_xml widget Other into it.
    Then I created a layout in Download_xml and added the newly created List widget list into it.
    So now I am showing a QListWidget in List Widget in Download_xml Widget in List Widget.
    Just LOL.
    I would really like to improve and simplify my code, so suggestions are very welcome
    Last edited by Archa4; 11th February 2011 at 09:27.

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

    Default Re: Problem showing the right thing.

    forget the thing with this constructor. Do you really need an own QWidget? does List have any ui things except the ListWidget?

    Quote Originally Posted by Archa4 View Post
    I cannot call ParseXml, because in the Download_xml there are 2 signals that have to appear before parseXML is called. If i just try ParseXml, it will parse an empty XML, that has not been filled, cause the ReadyRead signal has not appeared yet...
    ok. call a function "GetMovieVector()" which does everything that's necessary to create your movielist.

    Qt Code:
    1. Vector<Movie*> GetMovieVector(const QString& file)
    2. {
    3. Download_xml Other(str);
    4. return Other.GetMovieList();
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Problem showing the right thing.

    Let's go back to the beginning. Please state what is the ultimate result you want to achieve and describe (without posting code) how you currently do it.
    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.


  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: Problem showing the right thing.

    I need to create a list of customized_widgets. Right now I have an List class, where i want to show the list with it's widgets, i have an Download_xml class where i connect to the URL and parse the XML, i have an Object class Movie where i create new objects with data i get from Download_xml and I have Customize_widget class where i create the widgets using the data from QVector<Movie *> where all the data about those list items is stored.

    The Ultimate Problem i have - when i create a variable Download_xml like this:
    Download_xml *Other= new Download_xml(str);
    I cannot get any data from it for now, cause no signals are emitted (in particular the ReadyRead and requestFinished), that's why i cannot get any data from there. I found a way around this:
    I call a function from within the Download_xml, AFTER the signals were emitted and parseXML has finished it's job.

    Quote Originally Posted by FelixB View Post
    forget the thing with this constructor. Do you really need an own QWidget? does List have any ui things except the ListWidget?



    ok. call a function "GetMovieVector()" which does everything that's necessary to create your movielist.

    Qt Code:
    1. Vector<Movie*> GetMovieVector(const QString& file)
    2. {
    3. Download_xml Other(str);
    4. return Other.GetMovieList();
    5. }
    To copy to clipboard, switch view to plain text mode 
    And what am I supposed to write in GetMovieList?
    after:
    Download_xml Other(str);
    i cannot access any data from that Other, couse there is no signals emitted, cause the {...} block has not ended.
    But without the signals parseXML wont start...
    No signals -> no parseXML -> no data...

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

    Default Re: Problem showing the right thing.

    How does download_xml work? You create the object on the stack so unless the whole download is synchronous, it will go out of scope and be deleted.
    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.


  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: Problem showing the right thing.

    Em... I have no idea what u just said sorry i'm really noob. I found out there is such a thing as Qt less then 2 weeks ago... I can post the Download_xml.cpp here:

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

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

    Default Re: Problem showing the right thing.

    Quote Originally Posted by wysota View Post
    How does download_xml work? You create the object on the stack so unless the whole download is synchronous, it will go out of scope and be deleted.
    no, that was my (stupid) idea. I have no clue how this xml download-stuff works... Archa4 creates it on the heap, as shown in the first post.

  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: Problem showing the right thing.

    I was able to alter mine cod a bit, so i don't have to use 3 layouts now.
    When parsing finishes I emit a signal, that launches the function completeList.
    I had to declare in List.h
    Qt Code:
    1. Download_xml *Other;
    To copy to clipboard, switch view to plain text mode 

    and in list constructor I added the connect thing:
    Qt Code:
    1. connect(Other, SIGNAL(finish()),this, SLOT(completeList()));
    To copy to clipboard, switch view to plain text mode 

    Now i don't have to return the QVector<Movie *>, i just get it from Other variable.

    Still if there are any suggestions on further code improvement, post here!

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

    Default Re: Problem showing the right thing.

    Oh, the class uses QHttp. I suggest to use QNetworkAccessManager instead. Also don't read data incrementally from the response, wait until the reply is done and read all in one go.
    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.


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

    Default Re: Problem showing the right thing.

    What improvements does QNetworkAccessManager has, that would help me improve my project?

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

    Default Re: Problem showing the right thing.

    Countless From developer's point of view it lets you reduce your code significantly and make it less error prone.
    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.


  21. The following user says thank you to wysota for this useful post:

    Archa4 (11th February 2011)

Similar Threads

  1. KDChart. What do you thing about?
    By Nadia in forum Qt-based Software
    Replies: 1
    Last Post: 15th February 2011, 01:55
  2. showing icons - setPixmap problem
    By Tomasz in forum Newbie
    Replies: 4
    Last Post: 18th November 2010, 17:37
  3. Problem showing QTableView
    By weaver4 in forum Qt Programming
    Replies: 6
    Last Post: 23rd November 2009, 13:42
  4. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03
  5. Problem showing raw images
    By wkit23 in forum Qt Programming
    Replies: 5
    Last Post: 8th September 2006, 13:33

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
  •  
Qt is a trademark of The Qt Company.