我正在尝试在 QModelIndexes 列表上使用 concurrent::run() 。我尝试调用的函数如下所示:
writeXML(QModelIndex &index)
我的 map 代码如下:
QModelIndexList list;
QFutureWatcher<void> futureWatcher;
futureWatcher.setFuture(QtConcurrent::map(list, list->writeXML() ));
futureWatcher.waitForFinished();
我收到一个编译错误,提示“没有匹配的函数来调用 writeXML()。
我看过这个教程,我觉得它很有用:http://www.bogotobogo.com/Qt/Qt5_QtConcurrent_QFutureWatcher_QProgressDialog_map.php
但我还不明白索引是如何传递给函数的,在我的例子中是 writeXML() ?
我需要对上面的代码做什么才能至少编译?
最佳答案
QModelIndexList
只是一个typedef
对于 QList<QModelIndex>
当然还有QList
没有writeXML
方法,因为它是您的自定义函数。您需要一个容器 ( list
) 和一个函数 ( writeXML
),所以它应该是。
QModelIndexList list;
QFutureWatcher<void> futureWatcher;
futureWatcher.setFuture(QtConcurrent::map(list, writeXML));
//valid only if writeXML is a function, not a class member function!
futureWatcher.waitForFinished();
另一个例子:
QMutex mutex;
void writeXML(QModelIndex & index)
{
QMutexLocker lock(&mutex);
qDebug() << index.data();
}
//...
{
//somewhere
//get list of indexes
QModelIndexList list = ui->tableView->selectionModel()->selectedIndexes();
QFutureWatcher<void> futureWatcher;
//apply writeXML to each index
futureWatcher.setFuture(QtConcurrent::map(list, writeXML));
futureWatcher.waitForFinished();
}
关于c++ - 使用带有 QModelIndexes 列表的 concurrent::map(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117715/