Results 1 to 9 of 9

Thread: how to iterate over images and display them in sets of 8 (on QPushbuttons)

Hybrid View

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

    Default Re: how to iterate over images and display them in sets of 8 (on QPushbuttons)

    The "for" loop you have iterates over all images thus you see the last eight. You should have a single iteration modifying eight images starting from the one after the last displayed one.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Gallery : public QWidget {
    4. public:
    5. Gallery(const QString &dirName) {
    6. QGridLayout *l = new QGridLayout(this);
    7. for(int i=0;i<9;i++){
    8. QLabel *lab = new QLabel;
    9. l->addWidget(lab, i / 3, i % 3);
    10. lab->setFixedSize(200,200);
    11. lab->setAlignment(Qt::AlignCenter);
    12. labels << lab;
    13. }
    14. imagePaths = QDir(dirName).entryList(QStringList() << "*.jpg" << "*.png" << "*.bmp");
    15. m_dirName = dirName;
    16. m_nextImage = 0;
    17. }
    18. void start(int ms){
    19. m_timer = startTimer(ms);
    20. m_nextImage = 0;
    21. updateLabels();
    22. }
    23. void stop(){
    24. killTimer(m_timer);
    25. }
    26. protected:
    27. void timerEvent(QTimerEvent *tev){
    28. updateLabels();
    29. }
    30. void updateLabels(){
    31. for(int i=0;i<9;i++){
    32. if(m_nextImage>=imagePaths.size()){
    33. labels[i]->setPixmap(QPixmap());
    34. } else {
    35. labels[i]->setPixmap(QPixmap(m_dirName+QDir::separator()+imagePaths.at(m_nextImage)).scaled(QSize(200,200), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    36. }
    37. m_nextImage++;
    38. }
    39. if(m_nextImage>=imagePaths.size()) m_nextImage = 0;
    40. }
    41. private:
    42. QList<QLabel*> labels;
    43. int m_timer;
    44. int m_nextImage;
    45. QString m_dirName;
    46. QStringList imagePaths;
    47. };
    48.  
    49. int main(int argc, char **argv){
    50. QApplication app(argc, argv);
    51. Gallery gal(argv[1]);
    52. gal.show();
    53. gal.start(5000);
    54. return app.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    rishiraj (7th May 2009)

  3. #2
    Join Date
    Dec 2008
    Location
    My spaceship needs repairs..so, I am stuck on beautiful earth
    Posts
    98
    Thanks
    25
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Smile Solved

    Hi,
    Thanks Wysota.
    I modified the code as you suggested and, it's working properly for now.Will need to make a couple of changes to make it perfect...but,it's doing what I wanted it to do,that's iterate over the images and display them on the pushbuttons.
    Qt Code:
    1. void Mediagallery::updatepics()
    2. {
    3. timer->stop();
    4. int numimage=0;
    5. int count=(names.size()/8);
    6. if(counter<count) //counter has initial value '0'.
    7. {
    8. if(numimage<=8)
    9. {
    10. QString Imagename(*it5);
    11. NextImagepath = "/root/Desktop/photos/"+Imagename;
    12. QPixmap imagedisplay(NextImagepath);
    13.  
    14. QIcon icon(imagedisplay);
    15.  
    16. mainImage->setIcon(icon);
    17. numimage++;
    18.  
    19.  
    20.  
    21.  
    22. it5++;
    23.  
    24. QString Imagename2(*it5);
    25.  
    26. NextImagepath2 = "/root/Desktop/photos/"+Imagename2;
    27. QPixmap imagedisplay2(NextImagepath2);
    28.  
    29. bottmImage->setIcon(imagedisplay2);
    30. numimage++;
    31.  
    32.  
    33.  
    34. it5++;
    35.  
    36. QString Imagename3(*it5);
    37.  
    38. NextImagepath3 = "/root/Desktop/photos/"+Imagename3;
    39. QPixmap imagedisplay3(NextImagepath3);
    40.  
    41. leftImage1->setIcon(imagedisplay3);
    42. numimage++;
    43.  
    44.  
    45.  
    46. it5++;
    47.  
    48. QString Imagename4(*it5);
    49.  
    50. NextImagepath4 = "/root/Desktop/photos/"+Imagename4;
    51. QPixmap imagedisplay4(NextImagepath4);
    52.  
    53. leftImage2->setIcon(imagedisplay4);
    54. numimage++;
    55.  
    56.  
    57.  
    58. it5++;
    59.  
    60. QString Imagename5(*it5);
    61.  
    62. NextImagepath5 = "/root/Desktop/photos/"+Imagename5;
    63. QPixmap imagedisplay5(NextImagepath5);
    64.  
    65. leftImage3->setIcon(imagedisplay5);
    66. numimage++;
    67.  
    68.  
    69.  
    70. it5++;
    71.  
    72. QString Imagename6(*it5);
    73.  
    74. NextImagepath6 = "/root/Desktop/photos/"+Imagename6;
    75. QPixmap imagedisplay6(NextImagepath6);
    76.  
    77. rightImage1->setIcon(imagedisplay6);
    78. numimage++;
    79.  
    80.  
    81.  
    82. it5++;
    83.  
    84. QString Imagename7(*it5);
    85.  
    86. NextImagepath7 = "/root/Desktop/photos/"+Imagename7;
    87. QPixmap imagedisplay7(NextImagepath7);
    88.  
    89. rightImage2->setIcon(imagedisplay7);
    90. numimage++;
    91.  
    92.  
    93.  
    94. it5++;
    95.  
    96. QString Imagename8(*it5);
    97.  
    98. NextImagepath8 = "/root/Desktop/photos/"+Imagename8;
    99. QPixmap imagedisplay8(NextImagepath8);
    100.  
    101. rightImage3->setIcon(imagedisplay8);
    102. numimage++;
    103.  
    104. it5++;
    105.  
    106. counter++;
    107. timer1=new QTimer(this);
    108. connect(timer1,SIGNAL(timeout()),this,SLOT(updatepics()));
    109. timer1->start(7000);
    110.  
    111. }
    112. }
    113. else
    114. {
    115. timer1->stop();
    116. }
    117. }
    To copy to clipboard, switch view to plain text mode 
    If everything seems to be going well, you have obviously overlooked something.

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

    Default Re: Solved

    Don't you start a new timer at each call to updatepics()? After some time you will have lots of timers while you only need 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.


  5. #4
    Join Date
    Dec 2008
    Location
    My spaceship needs repairs..so, I am stuck on beautiful earth
    Posts
    98
    Thanks
    25
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to iterate over images and display them in sets of 8 (on QPushbuttons)

    true...no wonder I am getting segmentation faults.The first batch of 8 images and the last set of 8 images can be enlarged by clicking on them...but in the other batches,after I enlarge them,I get segmentation faults....I didn't check that out till now,so,will try to correct it.Thanks once again.
    If everything seems to be going well, you have obviously overlooked something.

Similar Threads

  1. Can QTextBrowser display remote images?
    By golnaz in forum Newbie
    Replies: 1
    Last Post: 21st January 2009, 13:45
  2. How to display a images use mosaic foramt?
    By longtrue in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 08:40
  3. Display multiple Images in a widget
    By jeetu_happy in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2007, 11:24
  4. How to display images in QTextBrowser ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 08:58

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.