[英]argument of type 'void (ClassName::)(QString&)' does not match 'void (ClassName::*)(QString&)'
我正在尝试使用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);
}
有任何想法吗?
这里有两件事。 首先,变量listof
是在foreach
循环内声明的,因此在创建future
时可能不可见。 其次,您应该使用&ClassName::processImage
作为QtConcurrent::map()
的第二个参数。
更新 :
查看文档,看来您需要编写调用以这种方式创建地图:
QFuture<void> future = QtConcurrent::map(listof, boost::bind(&ClassName::processImage, this));
(您必须使用boost.bind
将成员函数转换为普通函数,这就是map
期望的第二个参数)。
我认为它采用的是功能而不是功能地址。
QFuture<void> future = QtConcurrent::map(listof, &ClassName::processImage);
关于第二个错误:您不能像这样传递非静态成员函数。 map()不知道要调用哪个对象(该对象必须是ClassName类型)。
从QtConcurrentMap文档:
QtConcurrent :: map(),QtConcurrent :: mapped()和QtConcurrent :: mappedReduced()接受指向成员函数的指针。 成员函数类的类型必须与序列中存储的类型匹配:
您传递一个QString列表,但成员函数来自ClassName。 您可以使用boost :: bind(也可以使用STL的mem_fun / bind1st-fu)实现类似的功能:
...map( listof, boost::bind( &ClassName::processImage, this, _1 ) );
但这仍然行不通,因为您无法从UI线程以外的其他线程调用QTextEdit :: setText()。
另外,它应该是processImage(const QString&)而不是processImage(QString&)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.