Results 1 to 7 of 7

Thread: Beginner trying to iterate through image list, counter getting corrupted

  1. #1
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Beginner trying to iterate through image list, counter getting corrupted

    Hello all,

    I am a beginner learning Qt C++.
    I am making an application for Symbian that will teach the user how to tie knots. I am intending to iterate through a list of images and text strings to display them to the user via 2 QLabels. I would like to give the user an option to "play" or iterate automatically with an adjustable delay.
    The problem is whenever I stop play and then start again my counter (in changeImage() )gets screwed up and the the order of the images gets all messed up.
    PushButton_3 is the play pause button that calls the timer which calls the change image routine.
    I cant figure out where the counter is getting screwed up with this simple code... I am sure this is some basic mistake I am making like usual...

    Header for the knot view dialog;
    Qt Code:
    1. #ifndef KNOTVIEWDLG_H
    2. #define KNOTVIEWDLG_H
    3.  
    4. #include <QDialog>
    5. #include "whatknot.h"
    6. #include <QObject>
    7. #include <QTimer>
    8.  
    9. namespace Ui {
    10. class knotViewDlg;
    11. }
    12.  
    13. class knotViewDlg : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit knotViewDlg(QWidget *parent = 0);
    19. ~knotViewDlg();
    20. void keyPressEvent(QKeyEvent *);
    21. //current image name vari
    22. QString cntKnot;
    23. //Current list of image names
    24. QStringList strLst;
    25. int counter; // image list counter
    26. int delay; //image delay
    27. bool play; // is playing?
    28. // Timer
    29. QTimer* timer;
    30. private:
    31. Ui::knotViewDlg *ui;
    32. public slots:
    33. void getIndex(QString knot);
    34. void timing();
    35. void changeImage();
    36.  
    37. private slots:
    38. void on_pushButton_3_clicked();
    39. };
    40. #endif // KNOTVIEWDLG_H
    To copy to clipboard, switch view to plain text mode 

    And here is the cpp;
    Qt Code:
    1. #include "knotviewdlg.h"
    2. #include "ui_knotviewdlg.h"
    3. #include "ui_whatknot.h"
    4. #include "whatknot.h"
    5. #include <QStringList>
    6. #include <QDir>
    7. #include <QDebug>
    8. #include <QKeyEvent>
    9.  
    10. knotViewDlg::knotViewDlg(QWidget *parent) :
    11. QDialog(parent),
    12. ui(new Ui::knotViewDlg)
    13. {
    14. ui->setupUi(this);
    15. //init timer
    16. timer = new QTimer(this);
    17. play = false;
    18. delay = 1000; //image play delay default 1s
    19.  
    20. QStringList result;
    21. //setup d with the path to the resource images
    22. QDir d(":/knotImages/images");
    23. //populate result with all image names
    24. result = d.entryList();
    25. //search result for key word
    26. QString str1 = cntKnot; //set str1 with selected knot name
    27. QString str;
    28. strLst = result.filter(str1);
    29. //set first image before calling timer
    30. str = strLst.at(0);
    31. ui->label->setPixmap(QPixmap(QString::fromUtf8(
    32. ":/knotImages/images/").append(str)));
    33. }
    34.  
    35. knotViewDlg::~knotViewDlg()
    36. {
    37. delete ui;
    38. }
    39.  
    40. void knotViewDlg::getIndex(QString knot)
    41. {
    42. const QString cntKnot = knot;
    43. }
    44. void knotViewDlg::changeImage()
    45. {
    46. QString str = strLst.at(counter);
    47. ui->label->setPixmap(QPixmap(QString::fromUtf8(
    48. ":/knotImages/images/").append(str)));
    49. counter++;
    50. if(counter >= (strLst.count()))
    51. {
    52. counter = 0;
    53. }
    54.  
    55. }
    56.  
    57. void knotViewDlg::timing()
    58. {
    59. //default delay between images 1s
    60. timer->setInterval(delay);
    61. connect(timer, SIGNAL(timeout()),SLOT(changeImage()));
    62. timer->start();
    63. }
    64.  
    65. void knotViewDlg::on_pushButton_3_clicked()
    66. {
    67. if(play == false)
    68. {
    69. play = true;
    70. timing();
    71. }
    72. else
    73. {
    74. timer->stop();
    75. play = false;
    76. }
    77.  
    78. }
    To copy to clipboard, switch view to plain text mode 

    Any and all advice is greatly appreciated!
    Cheers,
    Jon

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Beginner trying to iterate through image list, counter getting corrupted

    Qt Code:
    1. void knotViewDlg::on_pushButton_3_clicked()
    2. {
    3. if(play == false)
    4. {
    5. play = true;
    6. counter = 0; // Do not forget to reset the counter
    7. timing();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to tbscope for this useful post:

    Jon Heron (11th October 2010)

  4. #3
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Re: Beginner trying to iterate through image list, counter getting corrupted

    Thanks!
    That will certainly work to reset the images from the start each time. However, I would like to be able to pause the iteration, not reset it with the pushButton.
    Something very strange is going on with the int counter variable after the iteration is paused. Once its started again the order of the images is completely messed up, it seems random too. Once and awhile after a pause and restart it will play the same 2 images over and over... I dont see how thats even possible with the code above... Its like something is messing up the order on the strLst?

    Cheers,
    Jon

  5. #4
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Re: Beginner trying to iterate through image list, counter getting corrupted

    Hi Jon,

    your code is a little bit unique. I will give you a sample:

    Instead ...
    Qt Code:
    1. void knotViewDlg::changeImage()
    2. {
    3. QString str = strLst.at(counter);
    4. ui->label->setPixmap(QPixmap(QString::fromUtf8(
    5. ":/knotImages/images/").append(str)));
    6. counter++;
    7. if(counter >= (strLst.count()))
    8. {
    9. counter = 0;
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    try it like this for example ...

    Qt Code:
    1. void knotViewDlg::changeImage()
    2. {
    3. label->setPixmap( QPixmap(QString::fromUtf8( strLst.at( counter ) ) ) );
    4.  
    5. ++counter;
    6. if( counter >= ( strLst.count() ) )
    7. counter = 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    And then add something like
    Qt Code:
    1. //for debugging only ...
    2. qDebug() << "current counter is:" << counter;
    3. qDebug() << "current image is:" << strLst.at( counter );
    To copy to clipboard, switch view to plain text mode 
    Regards
    Guenther

  6. The following user says thank you to gboelter for this useful post:

    Jon Heron (11th October 2010)

  7. #5
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Re: Beginner trying to iterate through image list, counter getting corrupted

    Thanks Guenther!
    Your debug output suggestion showed me that the order of the counter and strLst was correct, the signal to changeImage() was still getting called after timer->stop() was called. A simple edition of timer->disconnect(); did the trick!
    Qt Code:
    1. void knotViewDlg::on_pushButton_3_clicked()
    2. {
    3. if(play == false)
    4. {
    5. play = true;
    6. timing();
    7. }
    8. else
    9. {
    10. timer->stop();
    11. timer->disconnect(); //needed to stop signals
    12. play = false;
    13. }
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    Jon

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Beginner trying to iterate through image list, counter getting corrupted

    Each time timing() was called (i.e. when the button was pressed) you connect the timer's timeout() signal to your changeImage() slot again. Duplicate connections lead to duplicate signals. Connect the timer and slot once in the constructor and use the push button slot to toggle the timer start()/stop() (and adjust timer period if needed).

  9. The following user says thank you to ChrisW67 for this useful post:

    Jon Heron (11th October 2010)

  10. #7
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Lightbulb Re: Beginner trying to iterate through image list, counter getting corrupted

    Ahhhh! I get it!
    That makes perfect sense!
    Thanks Chris!
    Cheers,
    Jon

Similar Threads

  1. Hi Help me! Rev Counter developed
    By toro.86 in forum Jobs
    Replies: 2
    Last Post: 12th October 2010, 21:19
  2. Replies: 0
    Last Post: 14th July 2010, 13:48
  3. iterate through QHash
    By rosenth in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2010, 07:55
  4. corrupted double-linked list
    By reuabreliz in forum Installation and Deployment
    Replies: 2
    Last Post: 6th January 2010, 13:15
  5. Replies: 26
    Last Post: 21st July 2007, 21:34

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.