c++ - 使用带有 QModelIndexes 列表的 concurrent::map()

标签 c++ multithreading qt

我正在尝试在 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/

相关文章:

c++ - 是否有可用的*免费* QT UI 测试框架?

c++ - Const 变量随 C 中的指针改变

c++ - 为什么这个结构需要一个大小值?

c++ - Qt在定时器上改变图片

python - 如何异步执行多次函数并获得第一个结果

c# - 如何锁定具有相同ID的对象?

c++ - CComModule 取消注册服务器错误?

java - 私有(private) Java 类属性在方法调用之间神秘地重置

c++ - 更改 QTableView 中列的顺序

windows - 何时或如何删除 Qt 中的 QThread