[英]argument of type 'void (ClassName::)(QString&)' does not match 'void (ClassName::*)(QString&)'
Im trying to use Qt's qtconcurrentmap to process some images and I'm getting the following error 我正在尝试使用Qt的qtconcurrentmap处理一些图像,但出现以下错误
argument of type 'void (ClassName::)(QString&)' does not match 'void (ClassName::*)(QString&)
I'm also getting 我也越来越
/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 (...)'
This is my code 这是我的代码
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);
}
Any Ideas? 有任何想法吗?
Two things here. 这里有两件事。 First, the variable listof
is declared inside the foreach
loop, so it may not be visible at the point of creating future
. 首先,变量listof
是在foreach
循环内声明的,因此在创建future
时可能不可见。 Second, you should use &ClassName::processImage
as the second parameter to QtConcurrent::map()
. 其次,您应该使用&ClassName::processImage
作为QtConcurrent::map()
的第二个参数。
UPDATE : 更新 :
Looking at the documentation, it seems you need to write the call to create the map this way: 查看文档,看来您需要编写调用以这种方式创建地图:
QFuture<void> future = QtConcurrent::map(listof, boost::bind(&ClassName::processImage, this));
(you have to use boost.bind
to convert a member function into a normal function, that is what map
expect as the second argument). (您必须使用boost.bind
将成员函数转换为普通函数,这就是map
期望的第二个参数)。
我认为它采用的是功能而不是功能地址。
QFuture<void> future = QtConcurrent::map(listof, &ClassName::processImage);
About the second error: You can't pass a non-static member function like this. 关于第二个错误:您不能像这样传递非静态成员函数。 map() wouldn't know which object to call it on (which must be of type ClassName). map()不知道要调用哪个对象(该对象必须是ClassName类型)。
From QtConcurrentMap docs: 从QtConcurrentMap文档:
QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept pointers to member functions. QtConcurrent :: map(),QtConcurrent :: mapped()和QtConcurrent :: mappedReduced()接受指向成员函数的指针。 The member function class type must match the type stored in the sequence: " 成员函数类的类型必须与序列中存储的类型匹配:
You pass a QString list, but the member function is from ClassName. 您传递一个QString列表,但成员函数来自ClassName。 You can achieve something like this with boost::bind (and probably also with some mem_fun/bind1st-fu from the STL): 您可以使用boost :: bind(也可以使用STL的mem_fun / bind1st-fu)实现类似的功能:
...map( listof, boost::bind( &ClassName::processImage, this, _1 ) );
Still this won't work as you can't call QTextEdit::setText() from other threads than the UI thread. 但这仍然行不通,因为您无法从UI线程以外的其他线程调用QTextEdit :: setText()。
Also, it should be processImage(const QString&) instead of processImage(QString&). 另外,它应该是processImage(const QString&)而不是processImage(QString&)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.