简体   繁体   中英

How to use QtConcurrent to qCompress on QByteArray?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM