1

我想编写一个小程序,使用 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);

问题是编译器给我一个错误

第一:没有匹配的函数调用过滤(&QByteArray,)。

第二:请求从 QList 转换为非标量类型 QByteArray。

所以,我的问题是,有可能做我想做的事吗?

提前致谢

4

1 回答 1

1

不确定,如果 qt4 可以处理这个。

QList<QByteArray> list;
...add ByteArrays to list...
auto wordMapFn  = [](QByteArray &arr){arr=qCompress(arr, 9);};
QFuture<void> f = QtConcurrent::map(list,wordMapFn);

这会压缩列表中的所有 QByteArrays。如果要保留未压缩的数组,请使用映射而不是映射。wordMapFn 必须相应调整。如果您只想压缩单个 QByteArray QtConcurrent::run 可能更合适。

注意列表的生命周期。

于 2015-04-30T08:57:26.413 回答