I want to write a little program which compress all files from a directory by using qCompress of QByteArray.
However i want to run the compression on a multithreaded environment by using QtConcurrent. But i have some problems.
Here my code :
FilePool pool(folder,suffix);
QFutureWatcher<QString> watcher;
QProgressDialog progressDialog;
connect(&watcher,SIGNAL(progressRangeChanged(int,int)),&progressDialog,SLOT(setRange(int,int)));
connect(&watcher,SIGNAL(progressValueChanged(int)),&progressDialog,SLOT(setValue(int)));
connect(&progressDialog,SIGNAL(canceled()),&watcher,SLOT(cancel()));
QFuture<QString> future = QtConcurrent::filtered(pool,FindInFile(search));
QString text;
watcher.setFuture(future);
progressDialog.exec();
future.waitForFinished();
//Test for compressing file
QFile outFile("testCompress.ecf");
outFile.open(QIODevice::WriteOnly);
QByteArray nonCompressedData;
foreach(const QString &file,future.results()){
//Fichier d'entrée
QFile inFile(file);
inFile.open(QIODevice::ReadOnly);
nonCompressedData.append(inFile.readAll());
inFile.close();
text += file + "\n";
}
//QByteArray compressedData(qCompress(nonCompressedData,9));
//PROBLEM HERE
QFuture<QByteArray> futureCompressor = QtConcurrent::filtered(nonCompressedData,qCompress);
futureCompressor.waitForFinished();
QByteArray compressedData = futureCompressor.results();
outFile.write(compressedData);
The problem is that the compiler raise me an errors
First : No matching function for call to filtered(&QByteArray,).
Second : converstion from QList to non scalar type QByteArray requested.
So, my question is,is it possible to do what i want?
Thanks in advance
Not sure, if qt4 can handle this.
QList<QByteArray> list;
...add ByteArrays to list...
auto wordMapFn = [](QByteArray &arr){arr=qCompress(arr, 9);};
QFuture<void> f = QtConcurrent::map(list,wordMapFn);
This compresses all QByteArrays in list. If you want to keep your uncompressed arrays, use mapped instead of map. wordMapFn has to be adjusted accordingly. If you want to compress only a single QByteArray QtConcurrent::run might be more appropriate.
Pay attention to the life time of list.