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

Thread: Making a picture loop cant get the results im looking for (XML + QMAP)

  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 Making a picture loop cant get the results im looking for (XML + QMAP)

    Header File
    Qt Code:
    1. QMap<QString, QString> parsePics(QXmlStreamReader& xml2);
    2. void addElementDataToMap_Pics(QXmlStreamReader& xml2,
    3. QMap<QString, QString>& map2) const;
    4.  
    5. void addPicsToUI(QList< QMap<QString,QString> >& root2);
    6.  
    7. void addToList_Pics(QList< QMap<QString,QString> >& root2);
    8.  
    9. QLabel *picture;
    To copy to clipboard, switch view to plain text mode 

    CPP File
    Qt Code:
    1. QTimer::singleShot(0,this, SLOT(parseXML_Pics()));
    2.  
    3.  
    4. QTimer *timer = new QTimer(this);
    5. connect(timer, SIGNAL(timeout()), this, SLOT(doAddToList_Pics()));
    6. timer->start(3000);
    7.  
    8. void mediazone::doAddToList_Pics()
    9. {
    10. QList< QMap<QString,QString> > root2;
    11. addToList_Pics(root2);
    12. }
    13.  
    14. void mediazone::parseXML_Pics() {
    15. /* We'll parse the example.xml */
    16. QFile* file2 = new QFile("/home/dev/ttm/moives/xml/strings.xml");
    17. /* If we can't open it, let's show an error message. */
    18. if (!file2->open(QIODevice::ReadOnly | QIODevice::Text)) {
    19. QMessageBox::critical(this,
    20. "QXSRimpl::parseXML",
    21. "file2 Couldn't open strings.xml",
    22. return;
    23. }
    24. /* QXmlStreamReader takes any QIODevice. */
    25. QXmlStreamReader xml2(file2);
    26. QList< QMap<QString,QString> > root2;
    27. /* We'll parse the XML until we reach end of it.*/
    28. while(!xml2.atEnd() &&
    29. !xml2.hasError()) {
    30. /* Read next element.*/
    31. QXmlStreamReader::TokenType token2 = xml2.readNext();
    32. /* If token is just StartDocument, we'll go to next.*/
    33. if(token2 == QXmlStreamReader::StartDocument) {
    34. continue;
    35. }
    36. /* If token is StartElement, we'll see if we can read it.*/
    37. if(token2 == QXmlStreamReader::StartElement) {
    38. /* If it's named root, we'll go to the next.*/
    39. if(xml2.name() == "root") {
    40. continue;
    41. }
    42. /* If it's named row, we'll dig the information from there.*/
    43. if(xml2.name() == "row") {
    44. root2.append(this->parsePics(xml2));
    45. }
    46. }
    47. }
    48. /* Error handling. */
    49. if(xml2.hasError()) {
    50. QMessageBox::critical(this,
    51. "xml2 has error",
    52. xml2.errorString(),
    53. }
    54. /* Removes any device() or data from the reader
    55.   * and resets its internal state to the initial state. */
    56. xml2.clear();
    57. this->addToList_Pics(root2);
    58.  
    59. }
    60.  
    61. QMap<QString, QString> mediazone::parsePics(QXmlStreamReader& xml2) {
    62. QMap<QString, QString> row2;
    63. /* Let's check that we're really getting a row. */
    64. if(xml2.tokenType() != QXmlStreamReader::StartElement &&
    65. xml2.name() == "row") {
    66. return row2;
    67. }
    68. /* Let's get the attributes for row */
    69. QXmlStreamAttributes attributes2 = xml2.attributes();
    70. /* Let's check that row has id attribute. */
    71. if(attributes2.hasAttribute("id")) {
    72. /* We'll add it to the map. */
    73. row2["id"] = attributes2.value("id").toString();
    74. }
    75. /* Next element... */
    76. xml2.readNext();
    77. /*
    78.   * We're going to loop over the things because the order might change.
    79.   * We'll continue the loop until we hit an EndElement named row.
    80.   */
    81. while(!(xml2.tokenType() == QXmlStreamReader::EndElement &&
    82. xml2.name() == "row")) {
    83. if(xml2.tokenType() == QXmlStreamReader::StartElement) {
    84. /* We've found Picture. */
    85. if(xml2.name() == "zone2banner300x300") {
    86. this->addElementDataToMap_Pics(xml2, row2);
    87. emit doneParsePics();
    88. }
    89. }
    90. /* ...and next... */
    91. xml2.readNext();
    92. }
    93. return row2;
    94. }
    95.  
    96. void mediazone::addElementDataToMap_Pics(QXmlStreamReader& xml2,
    97. QMap<QString, QString>& map2) const {
    98. /* We need a start element, like <foo> */
    99. if(xml2.tokenType() != QXmlStreamReader::StartElement) {
    100. return;
    101. }
    102. /* Let's read the name... */
    103. QString elementName2 = xml2.name().toString();
    104. /* ...go to the next. */
    105. xml2.readNext();
    106. /*
    107.   * This elements needs to contain Characters so we know it's
    108.   * actually data, if it's not we'll leave.
    109.   */
    110. if(xml2.tokenType() != QXmlStreamReader::Characters) {
    111. return;
    112. }
    113. /* Now we can add it to the map.*/
    114. map2.insert(elementName2, xml2.text().toString());
    115. }
    116.  
    117. void mediazone::addToList_Pics(QList< QMap<QString,QString> >& root2)
    118. {
    119. while(!root2.isEmpty()) {
    120.  
    121. QMap<QString,QString> row2 = root2.takeFirst();
    122. QMapIterator<QString,QString> Iter_Pics(row2);
    123. while(Iter_Pics.hasNext())
    124. {
    125. Iter_Pics.next();
    126. QString myPics;
    127. myPics = Iter_Pics.value();
    128.  
    129. QString vidpath_Pics = QApplication::applicationDirPath();
    130. vidpath_Pics = "/home/dev/ttm/images/" + myPics;
    131.  
    132. picture = new QLabel(this);
    133.  
    134. picture->setGeometry(QRect(781,2,475,360));
    135.  
    136. picture->clear();
    137. picture->setPixmap(QPixmap(vidpath_Pics));
    138. picture->showFullScreen();
    139. picture->show();
    140.  
    141. }
    142.  
    143. }
    144.  
    145. }
    To copy to clipboard, switch view to plain text mode 

    my code above does read from the xml file and grabs the picture text and then goes to the folder and grabs the file its supposed to rotate every 15 seconds starting with the first then when at the end do it again..

    but it wont rotate nor start again if you reach the end...

    please anyone can help thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Line 8, "doAddToList_Pics" always passes an empty map to the "addToList_Pics" method, so the method does nothing on timeout. And your timer is set for 3 seconds, not 15 seconds.

  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: Making a picture loop cant get the results im looking for (XML + QMAP)

    how would i pass it to a slot then cause i cant pass the timer slot to addToList_Pics cause its not a slot :S how would i emit the timer to addToList_Pics

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    You need to make "root2" a member variable of your "mediazone" class, and remove lines 10 and 27 that create temporary QLists on the stack.

    But your code has much bigger problems:

    - Line 34 is a huge memory leak, because every time through the loop you create a new QLabel, but the old pointer is never deleted. What I think you want to do is simply *replace* the image on the label, not make a whole new label widget each time the image changes, right? Create the "picture" QLabel in the constructor and that's all you need (move line 34 to the constructor).

    - The loop in lines 25 - 43 will execute almost instantly, so you will never see the pictures "rotating" through a sequence. The way your code is written now (new QLabel picture every time through the loop), you will get a whole bunch of QLabel instances on the screen, instantly, each with a different picture. If you move the code to create a new QLabel out of this loop and reuse the same QLabel, then you'll see a brief flicker as each picture is loaded and replaced, then the final picture will be displayed until the timer fires again.

    So, you need to think about the logic of what you want to do. If the goal is to rotate through a series of pictures, and repeat the series every 15 seconds, then you need probably 2 timers: one to change from one picture to the next and leave it visible for a second or two, and another timer to repeat the whole thing every 15 seconds. If it takes more than 15 seconds to go through the whole list of pictures, then you have a problem, because the second timer will fire before the rotation is done. If it takes less than 15 seconds, then the last picture will stay up until the 15 second timer fires again.

    If what you really want to do is to continuously rotate through the series of pictures, however long it takes, then you need one timer. Each time this timer fires, you display the next picture in the list. If you get to the end of the list, go back to the beginning again.

  5. The following user says thank you to d_stranz for this useful post:

    prophet0 (28th November 2011)

  6. #5
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    Quote Originally Posted by d_stranz View Post
    If what you really want to do is to continuously rotate through the series of pictures, however long it takes, then you need one timer. Each time this timer fires, you display the next picture in the list. If you get to the end of the list, go back to the beginning again.
    Yes this is what im trying to do create 1 label show 1 picture for 15 seconds then show the next one from xml once its finished start at the beginning...

    im fairly new to Qt..

    thanks for your prompt reply

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Yes this is what im trying to do create 1 label show 1 picture for 15 seconds then show the next one from xml once its finished start at the beginning...
    Then you need to do something like this:

    Qt Code:
    1. - Load picture file names from XML into root2
    2. - currentPictureIndex = 0
    3. - Create QLabel
    4. - Create timer
    5. - Connect timer timeout to slot "showNextPicture"
    6. - Start timer with 15000 ms timeout
    7.  
    8. - showNextPicture()
    9. currentPictureIndex++
    10. if ( currentPictureIndex == root2.size() )
    11. currentPictureIndex = 0;
    12. read picture at root2[ currentPictureIndex ]
    13. show picture on label
    To copy to clipboard, switch view to plain text mode 

    But why not just load all the pictures into an array of QImage instead of repeatedly reading them from the files?

  8. The following user says thank you to d_stranz for this useful post:

    prophet0 (28th November 2011)

  9. #7
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    i been trying various ways i have added root2 as a member of mediazone and changed a few things but cant really get a grasp on how to make it work correctly and clean no memory leaks

    the files change all the time so the xml is created by a sql database and then the program reads from the xml but the program also downloads the files from the xml and then plays the pictures more information will be pulled from xml for this later on but this is how im trying to make it work for now

  10. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Then you want to change the "code" I posted to move the loading of pictures into a slot that gets called each time the XML changes. In that slot, you should

    Qt Code:
    1. - stop the timer
    2. - clear root2
    3. - read new pictures into root2
    4. - set currentPictureIndex = 0
    5. - start the timer again
    To copy to clipboard, switch view to plain text mode 

    Stopping and restarting the timer may not be strictly necessary, but it ensures that you don't try to display a picture at an invalid index. In the "showNextPicture" slot, you should also check first to see if root2 is empty before trying to display a picture.

  11. #9
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    im only stuck on the

    read picture at root2[ currentPictureIndex ]
    show picture on label

    in the slot shotNextPicture

  12. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Qt Code:
    1. void showNextPicture()
    2. {
    3. if( !root2.isEmpty() )
    4. {
    5. QMap<QString,QString> row2 = root2.takeFirst();
    6. if ( !row2.isEmpty() )
    7. {
    8. currentPictureIndex++;
    9. if ( currentPictureIndex == row2.size() )
    10. currentPictureIndex = 0;
    11.  
    12. QMapIterator<QString,QString> Iter_Pics(row2) + currentPictureIndex;
    13.  
    14. QString myPics;
    15. myPics = Iter_Pics.value();
    16.  
    17. QString vidpath_Pics = QApplication::applicationDirPath(); // why are you doing this
    18. vidpath_Pics = "/home/dev/ttm/images/" + myPics; // when this trashes whatever you just assigned above?
    19.  
    20. picture->setGeometry(QRect(781,2,475,360)); // why are you setting this geometry
    21.  
    22. picture->clear();
    23. picture->setPixmap(QPixmap(vidpath_Pics));
    24. picture->showFullScreen(); // only to then show it full screen?
    25. picture->show();
    26.  
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Obviously untested, but you should be doing something like this.

  13. The following user says thank you to d_stranz for this useful post:

    prophet0 (28th November 2011)

  14. #11
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    line 17: thought it was necessary

    fullscreen i was meaning to remove that due to the fact i setgeometry

    QMapIterator<QString,QString> Iter_Pics(row2) + currentPictureIndex;

    mediazone.cpp:315: error: expected ‘,’ or ‘;’ before ‘+’ token

  15. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Replace:
    Qt Code:
    1. QMapIterator<QString,QString> Iter_Pics(row2) + currentPictureIndex;
    To copy to clipboard, switch view to plain text mode 

    with:

    Qt Code:
    1. QMap<QString, QString>::const_iterator Iter_Pics = row2.begin() + currentPictureIndex;
    To copy to clipboard, switch view to plain text mode 

    line 17: thought it was necessary
    Necessary for what? You retrieve the application directory path and assign it to vidpath_Pics in line 17. In the very next line (18), you assign vidpath_Pics to a different string. So the first assignment doesn't do anything useful. If what you intend to do is to assign vidpath_Pics to the concatenation of your application directory path + the current picture file name, then you want this:

    Qt Code:
    1. QString vidpath_Pics = QApplication::applicationDirPath() + myPics;
    To copy to clipboard, switch view to plain text mode 

    and delete line 18.

  16. #13
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    ok so i did fix those lines

    but it was returning empty i added the code into my addToList_Pics with the doAddToList_Pics slot and its working with the time new value everytime ... only the label nothing appears .. i think i messed up the qlabel in the constructor


    in my cpp i have

    picture = new Qlabel(this);

    then in the slot i have everything to show but maybe not getting passed ?

    again thank you for your help

  17. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Your constructor needs this:

    Qt Code:
    1. picture = new QLabel(this);
    To copy to clipboard, switch view to plain text mode 

    and make sure "picture" is a member variable for your mediazone class.

    Are you sure that the picture actually gets loaded from the file? Try changing it to this:

    Qt Code:
    1. QPixmap pixmap;
    2. if ( pixmap.load( vidpath_Pics ) )
    3. picture->setPixmap( pixmap );
    4. else
    5. // error, pixmap didn't load properly
    To copy to clipboard, switch view to plain text mode 

  18. #15
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    After changing the code to yours application starts then exits

    QLabel *picture;
    is in my header file for mediazone yes

  19. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Are you running this in a debugger? Obviously something is crashing somewhere, but if you are making a release mode executable and running that, you can never find out where. Make a debug version, run it in the debugger, see where the crash occurs, and figure out why.

    If you aren't calling "picture = new QLabel( this )" in your constructor, then "picture" will be uninitialized when you try to use it, and will cause a crash. Putting the declaration in the header file doesn't create the QLabel instance, it just declares the variable. Show the code in your constructor that create the QLabel.

  20. #17
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    its saying vidpath_pics is not accessable

  21. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    I don't have any idea what you're talking about now. First you said the app was crashing, now you tell me there's a compiler error. I have no idea what you have changed or where.

    If the compiler is telling you that some variable is not accessible, then it means you have declared the variable outside the scope of where you are trying to use it (or have not declared it at all). And in your last note, you said "vidpath_pics", but in your original code, it was "vidpath_Pics". Did you simply make a typo here or in your code?

  22. #19
    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: Making a picture loop cant get the results im looking for (XML + QMAP)

    this is what the debugger has to say when it segfault

    Qt Code:
    1. Locals
    2. Iter_Pics
    3. myPics ""
    4. root2 <9 items>
    5. row2 <1 items>
    6. this
    7. _layout
    8. o 0x0
    9. aio_audioOutput
    10. currentPictureIndex -1225357119
    11. mediaObject
    12. metaInformationResolver
    13. myPics ""
    14. nam
    15. picture
    16. root2 <9 items>
    17. sources <0 items>
    18. ui
    19. wid_videowidget
    20. xml
    21. d_ptr
    22. vidpath_Pics <not accessible>
    23. Watchers
    To copy to clipboard, switch view to plain text mode 


    Added after 6 minutes:


    sorry i ment vidpath_Pics


    Added after 4 minutes:


    So i commented this line

    //QString myPics;
    //myPics = Iter_Pics.value();

    and the application runs as it should ..... something went wrong here :S


    Added after 41 minutes:


    Qt Code:
    1. QString myPics;
    2. myPics = Iter_Pics.value();
    To copy to clipboard, switch view to plain text mode 

    is causing the problem and i have no idea why

    when i start my application it starts grey then segfault

    when i start with the lines comment out application works as normal!!

    hope this helps ....
    Last edited by prophet0; 28th November 2011 at 21:32.

  23. #20
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making a picture loop cant get the results im looking for (XML + QMAP)

    Looking at debugger output doesn't tell me anything about your *code*. And if you aren't retrieving anything into "myPics" from your "row2" iterator, but you say your application runs as it should, then I have no idea what you are actually doing in the code. It has probably changed a lot from your original post.

    Your original code had "myPics" containing the name of a picture file. If you are now ignoring the name, how does the picture get retrieved from the file?

    I can't help you without seeing what you are doing in the code.

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. How to create PIP (Picture In Picture) on QwebView
    By luckychap in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2011, 12:47
  3. Replies: 3
    Last Post: 18th February 2010, 15:01
  4. Making a reflection of a picture
    By desch in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2008, 18:38
  5. Making 3D controls in QT, implementing a game loop in QT
    By smurrish in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2006, 04:37

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.