I am trying to load multiple images using multithreading through QFutureWatcher class but I am not being able to do it properly. My code read the url of the images and save them on a QVector. Then I Pass these QStrings to a function that load, resize and save the images on a QMap object. The code is:
void MainWindow::loadImages(){
mImagesLoaderfutureWatcher = new QFutureWatcher<QString>() ;
connect(mImagesLoaderfutureWatcher, SIGNAL(finished()),this, SLOT(imageLoadingfinished()));
QVector<QString> imagesList = mProject->getImagesFileName();
// Start the 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 that I am receiving when I try to compile said that the list of arguments in the call to the function "addImageThumnailToMap" is not found but I think that it is not neccesary specify that on the "QtConcurrent::map()" function. Thanks for your help
It's because the function is a member of a class. You should call it like this:
It also looks like QtConcurrent::map takes only global functions or static functions, because there is no pointer to instance to call
addImageThumnailToMap
. So declearMainWindow::addImageThumnailToMap
as static.I personally prefer to use
QtConcurrent::run
, which can handle class member functions. E.g:The template parameters just happened to be int, because the function loadImages returns the number of actually loaded images.