qt - Problems developing QFutureWatcher to improve image loading times -
i trying load multiple images using multithreading through qfuturewatcher class not being able properly. code read url of images , save them on qvector. pass these qstrings function load, resize , save images on qmap object. code is:
void mainwindow::loadimages(){ mimagesloaderfuturewatcher = new qfuturewatcher<qstring>() ; connect(mimagesloaderfuturewatcher, signal(finished()),this, slot(imageloadingfinished())); qvector<qstring> imageslist = mproject->getimagesfilename(); // start computation. mimagesloaderfuturewatcher->setfuture(qtconcurrent::map(imageslist,addimagethumnailtomap)); } void mainwindow::addimagethumnailtomap(qstring imagename){ qsize desiredsize(100,100); qimage orig(mproject->getbasepath()+"/"+imagename); qimage scaled = orig.scaled( desiredsize, qt::keepaspectratio, qt::smoothtransformation); mmapimages->insert(imagename,scaled); } void mainwindow::imageloadingfinished(){ qmessagebox msg; msg.settext("images loaded"); msg.show(); }
the error receiving when try compile said list of arguments in call function "addimagethumnailtomap" not found think not neccesary specify on "qtconcurrent::map()" function. help
it's because function member of class. should call this:
mimagesloaderfuturewatcher->setfuture( qtconcurrent::map(imageslist,&mainwindow::addimagethumnailtomap));
it looks qtconcurrent::map takes global functions or static functions, because there no pointer instance call addimagethumnailtomap
. declear mainwindow::addimagethumnailtomap
static.
i prefer use qtconcurrent::run
, can handle class member functions. e.g:
void imagegroupview::setimages( qstringlist il ) { //qfuture<int> _imageloader; //qfuturewatcher<int> _watcher; _imageloader = qtconcurrent::run(this, &imagegroupview::loadimages, il ); connect(&_watcher, signal(finished()), this, slot(processimagesready())); _watcher.setfuture( _imageloader ); }
the template parameters happened int, because function loadimages returns number of loaded images.
int imagegroupview::loadimages( qstringlist il ) { int numloaded=0; _images.clear(); foreach ( qstring img, il ) { if( qfileinfo( img ).exists() ) { _imagelist.append( qimage( img ).scaled(640,480) ); numloaded++; } } return numloaded; }
Comments
Post a Comment