【发布时间】:2012-04-06 06:58:36
【问题描述】:
我正在尝试使用 QtConcurrent::map 来运行这个函数
//This function is used through QtConcurrent::map to create images from a QString path
void MainWindow::createQImage(QString* path) {
//create an image from the given path
QImage* t = new QImage(*path);
imageList->append(t);
}
在这个容器/序列上(在主窗口标题中声明并在主窗口构造函数中初始化)
QList<QImage *> *imageList = new QList<QImage *>;
这是我要运行的代码
QFutureWatcher<void> futureWatcher;
futureWatcher.setFuture(QtConcurrent::map(imageList, &MainWindow::createQImage));
这是我得到的错误:
request for member 'begin' in 'sequence', which is of non-class type 'QList<QImage*>*'
request for member 'end' in 'sequence', which is of non-class type 'QList<QImage*>*'
我需要为“imageList”中的每个元素运行“createQImage”函数,该函数可以达到数千个。我认为问题出在 map 函数的第一个参数上。从我读过的内容来看,这可能与兼容性有关。网上没有太多我能够与之相关的示例代码。我是 Qt 新手,不是最有经验的程序员,但我希望能得到一些帮助和反馈。
或者,有没有更好的方法使用 QtConcurrent 来做到这一点?
提前致谢!
【问题讨论】:
-
附带说明,如果正确实施(请参阅其他答案),这将无济于事。
QtConcurrent::map()迭代给定列表中的每个元素。由于您只在 map-function 中创建和附加元素,因此您的 imageList 最初将为空,因此您的createQImage()将永远不会被调用。相反,您可以使用非空的、空填充的QVector<QImage*> imageList(numberOfImagesYouNeed, Q_NULLPTR)并将其传递给 map()。
标签: qt concurrency qimage qtconcurrent