【发布时间】:2015-07-08 12:04:37
【问题描述】:
我想编写一个小程序,使用 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。
所以,我的问题是,有没有可能做我想做的事?
提前致谢
【问题讨论】:
-
当然可以在单独的线程中压缩数组。但是你尝试这样做的方式......它闻起来有点像'让我们从 QtConcurrent 中获取一个随机函数,随机传递一些参数给它,并希望最好的'
-
那么,你能引导我找到最好的方法吗?
-
您可以编写一个函数,使用 qCompress 压缩您的 QByteArray,然后使用 QtConcurrent::run 在另一个线程中执行此函数。
-
QtConcurrent::filtered 有一个完全不同的用例。 qCompress 过滤器功能错误,签名错误。即使 QtComcurrent::filtered 可以接受 qCompress,filtered 也会一个字节一个字节地给它 QByteArray ...... qCompress 不占用单个字节。
-
QtConcurrent::map + QByteArray 的向量至少是正确的方向。我不认为你可以直接使用 qCompress 作为 MapFunctor...需要是 U 函数(const T &t);。我认为,包装 qCompress 的 lambda 函数会很优雅。
标签: c++ qt c++11 qbytearray qtconcurrent