Results 1 to 9 of 9

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

  1. #1
    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

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

    Hi everyone,
    I have a picture gallery application in which there are 8 QPushbuttons on top of which the pixmaps are set.On clicking the buttons(once the pixmaps are set on them),one can view them in enlarged form and do the usual things like rotate,zoomin/out etc.
    My problem is that I have got more than 8 images in the picture folder so,I want to iterate over the images and display them in batches of 8...meaning,first time,it shows the first 8 images then after a couple of secs it shows the next 8 and so on till all the pics have been shown and then it again starts from the first 8 and goes on(till the user clicks on any of the pics to enlarge it).
    I tried the following code:-
    Qt Code:
    1. void Photogallery::createbuttons() //function called from constructor to create the
    2. //8 pushbuttons
    3. {
    4. <<<<code to create the 8 buttons>>>
    5. QDir CurrentDirectory("/root/Desktop/photos"); //'photos' dir. contains pictures
    6. names= CurrentDirectory.entryList(QDir::Files|QDir::NoDotAndDotDot);
    7. //'names' is a Qstringlist variable
    8. timer=new QTimer(this);
    9. connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    10. timer->start();
    11. }
    12.  
    13. void Mediagallery::update() //slot which sets up the images on buttons
    14. {
    15.  
    16. it= names.begin(); //'it' is QStringlist iterator
    17. int count=(names.size()/8);
    18.  
    19. for(int numimage=0;numimage<count;numimage++)
    20. {
    21. //first picture on first button.
    22. QString Imagename(*it5);
    23. firstImagepath = "/root/Desktop/photos/"+Imagename;
    24. QPixmap imagedisplay(firstImagepath);
    25. QIcon icon(imagedisplay);
    26. firstImage->setIcon(icon); //firstImage is a Qpushbutton
    27. connect(firstImage,SIGNAL(pressed()),this,SLOT(buttondown()));
    28. //buttondown() is a slot where I identify which button is clicked.
    29. //***********************************************************
    30. //second picture on second button
    31. it5++;
    32. QString Imagename2(*it5);
    33. secondImagepath = "/root/Desktop/photos/"+Imagename2;
    34. QPixmap imagedisplay2(secondImagepath);
    35. secondImage->setIcon(imagedisplay2);
    36. connect(secondImage,SIGNAL(pressed()),this,SLOT(buttondown()));
    37. //*************************************************************
    38. //third picture on third pushbutton
    39. it5++;
    40. QString Imagename3(*it5);
    41. thirdImagepath= "/root/Desktop/photos/"+Imagename3;
    42. QPixmap imagedisplay3(thirdImagepath);
    43. thirdImage->setIcon(imagedisplay3);
    44. connect(thirdImage,SIGNAL(pressed()),this,SLOT(buttondown()));
    45.  
    46. .........so on till 8th image is set.
    47.  
    48. QString numimagestr=QString::number(numimage);
    49. QMessageBox::information(NULL,"",numimagestr); //keeping count
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    If I remove the 'QMessageBox' statement towards the end of the loop,all I get are eight empty pushbuttons (No pictures are set on them).If I keep the QMessageBox,I get the images in batches on the buttons(as expected) but,I cannot click on any of the pictures to enlarge it as the MessageBox keeps coming in the foreground.
    I want the pics to show in batches of 8 without the presence of the MessageBox.
    Any ideas on how to do that(maybe I need to change the logic) or use a Timer or something else at some point ....Need your advice on how to proceed.
    Thank you.
    Last edited by rishiraj; 7th May 2009 at 11:12.
    If everything seems to be going well, you have obviously overlooked something.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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)

    What is the base class of your Mediagallery class?
    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
    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)

    QMainWindow()
    If everything seems to be going well, you have obviously overlooked something.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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)

    In that case you have to change the name of the slot from update() to something else. There already exists such a slot and it does something important so better rename yours to something else.

    And setup the connections between signals and slots once instead of doing it each time you update the images.
    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. #5
    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)

    Thank you,there's some improvement now.
    Changed 'update()' to 'updatepics()' and moved the signals-slots to the constructor.
    Now,If I don't use the 'QMessageBox',the last eight images in the folder are displayed on the QPushButtons and I can click on them and enlarge them(as is the plan).
    But,the other images(before the last eight) are not shown...how can I modify the code so that it shows all the images from first to last in batches of 8 and not just the last 8 images...something like display the first 8 images then wait for 5 seconds,show the second set and so on......any ideas/suggestions are welcome.
    Thank you.
    If everything seems to be going well, you have obviously overlooked something.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    rishiraj (7th May 2009)

  8. #7
    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  10. #9
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.