[英]How to use QtConcurrent to qCompress on QByteArray?
我想編寫一個小程序,使用QByteArray的qCompress壓縮目錄中的所有文件。
但是我想通過使用QtConcurrent在多線程環境上運行壓縮。 但是我有一些問題。
這是我的代碼:
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);
問題是編譯器向我提出錯誤
首先:沒有匹配函數可用於調用filter(&QByteArray,)。
第二:從QList到非標量類型QByteArray的會話請求。
所以,我的問題是,有可能做我想做的嗎?
提前致謝
不確定,qt4是否可以處理此問題。
QList<QByteArray> list;
...add ByteArrays to list...
auto wordMapFn = [](QByteArray &arr){arr=qCompress(arr, 9);};
QFuture<void> f = QtConcurrent::map(list,wordMapFn);
這將壓縮列表中的所有QByteArrays。 如果要保留未壓縮的數組,請使用maped而不是map。 wordMapFn必須進行相應的調整。 如果只想壓縮單個QByteArray QtConcurrent :: run,則可能更合適。
注意清單的使用壽命。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.