c++ - Using QpushButton to switch the images displaying in a QLabel -


i beginner. 1 part of project using qpushbutton switch images displaying in qlabel, first step open folder , set filter .jpg , save path in qstring list. here code:

void data_labeling::on_next_clicked() {     int = 0;      qstring filename1 = "/home/jin/test/test.jpg";     qfileinfo fileinfo1(filename1);     qstring foldername1 = fileinfo1.path();      qdir dir(foldername1);     dir.setnamefilters(qstringlist()<< "*.jpeg" << "*.jpg");     qstringlist images = dir.entrylist();      qimage image(images[i]);      qpixmap::fromimage(image);     int w = ui->face_pic->width();     int h = ui->face_pic->height();     ui->face_pic->setpixmap(qpixmap::fromimage(image).scaled(h,w,qt::keepaspectratio)); } 

there many images in folder,i know why failed, since every time press button using function, integer equal 0. can give me suggestion?

in order select folder recommend using qfiledialog class through getexistingdirectory() method. improvement generate function takes care of placing new image.

one more note entrylist() returns names of filter files, not full path, use filepath() function.

*.h

private:     qdir dir;     qstringlist images;     int index;      void updatepixmap(); 

*.cpp

{//constructor     ui->setupui(this);      index = 0;     qstring dirname = qfiledialog::getexistingdirectory(this, "select directory", "/home/jin/test/");     if(!dirname.isempty()){         dir = qdir(dirname);         dir.setnamefilters(qstringlist()<< "*.jpeg" << "*.jpg");         images = dir.entrylist();     }      updatepixmap(); }   void data_labeling::on_next_clicked() {     updatepixmap(); }  void data_labeling::updatepixmap() {     if(images.count() > 0){         int w = ui->face_pic->width();         int h = ui->face_pic->height();          qimage image(dir.filepath(images[index]));          ui->face_pic->setpixmap(qpixmap::fromimage(image).scaled(h,w,qt::keepaspectratio));         index  = (index +1) % images.count();     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -