[英]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.