我正在尝试使用Qt的qtconcurrentmap来处理一些图像,并收到以下错误
argument of type 'void (ClassName::)(QString&)' does not match 'void (ClassName::*)(QString&)我也得到了
/Library/Frameworks/QtCore.framework/Headers/qtconcurrentmapkernel.h::
In member function 'bool QtConcurrent::MapKernel<Iterator, MapFunctor>::runIteration(Iterator, int, void*)
[with Iterator = QList<QString>::iterator, MapFunctor = void (ClassName::*)(QString&)]':
/Library/Frameworks/QtCore.framework/Headers/qtconcurrentmapkernel.h:73:
error: must use '.*' or '->*' to call pointer-to-member function in
'((QtConcurrent::MapKernel<QList<QString>::iterator, void (ClassName::*)(QString&)>*)this)->QtConcurrent::MapKernel<QList<QString>::iterator, void (ClassName::*)(QString&)>::map (...)'这是我的代码
void ClassName::processImage(QString &f)
{
Image image;
image.read(qPrintable(f));
try {
//Apply effects on an Image
} catch ( Magick::Exception & error) {
// Displaying any possible errors on the text browser
ui->textBrowser_error->setText(error.what());
}
}
void ClassName::processAll() {
foreach (QFileInfo f, list)
{ QString str(f.absoluteFilePath());
QList<QString> listof;
listof.append(str);
}
QFuture<void> future = QtConcurrent::map(listof, processImage);
}有什么想法吗?
发布于 2010-12-09 05:11:38
这里有两件事。首先,变量listof是在foreach循环中声明的,因此在创建future时可能看不到它。其次,您应该使用&ClassName::processImage作为QtConcurrent::map()的第二个参数。
更新
查看文档,您似乎需要以这种方式编写调用来创建映射:
QFuture<void> future = QtConcurrent::map(listof, boost::bind(&ClassName::processImage, this));(您必须使用boost.bind将成员函数转换为普通函数,这就是map期望的第二个参数)。
发布于 2010-12-09 05:10:48
我认为它接受的是一个函数,而不是函数地址。
QFuture<void> future = QtConcurrent::map(listof, &ClassName::processImage);
发布于 2010-12-09 09:01:39
关于第二个错误:你不能像这样传递一个非静态成员函数。map()不知道在哪个对象上调用它(必须是ClassName类型)。
来自QtConcurrentMap文档:
QtConcurrent::map()、QtConcurrent::map()和QtConcurrent::mappedReduced()接受指向成员函数的指针。成员函数类类型必须与序列中存储的类型匹配:“
您传递了一个成员列表,但成员函数来自ClassName。您可以使用boost::bind (可能还可以使用STL中的mem_fun/bind1st fu)来实现类似的操作:
...map( listof, boost::bind( &ClassName::processImage, this, _1 ) );这仍然不会起作用,因为您不能从UI线程以外的其他线程调用QTextEdit::setText()。
同样,它应该是processImage(const QString&)而不是processImage(QString&)。
https://stackoverflow.com/questions/4392352
复制相似问题