如何使用 QtConcurrent 对 QByteArray 进行 qCompress?
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);
问题是编译器报错了
首先:没有匹配函数来调用 filtered(&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);
这将压缩列表中的所有 QByteArray。如果您想保留未压缩的数组,请使用映射而不是映射。 wordMapFn 必须相应地进行调整。如果只想压缩单个 QByteArray QtConcurrent::run 可能更合适。
注意list的生命周期
我想编写一个小程序,使用 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);
问题是编译器报错了
首先:没有匹配函数来调用 filtered(&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);
这将压缩列表中的所有 QByteArray。如果您想保留未压缩的数组,请使用映射而不是映射。 wordMapFn 必须相应地进行调整。如果只想压缩单个 QByteArray QtConcurrent::run 可能更合适。
注意list的生命周期