【问题标题】:Error with sequence argument when using QtConcurrent map使用 QtConcurrent 映射时序列参数出错
【发布时间】: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&lt;QImage*&gt; imageList(numberOfImagesYouNeed, Q_NULLPTR) 并将其传递给 map()。

标签: qt concurrency qimage qtconcurrent


【解决方案1】:

QtConcurrent::map 想要一个序列作为它的第一个参数。你向它传递了一个 指针 到一个序列。

如果你这样做

futureWatcher.setFuture(QtConcurrent::map(*imageList, &MainWindow::createQImage));

应该很开心。

请注意,编译器相当清楚问题所在。花点时间仔细阅读错误,它们通常不像最初看起来那样神秘。在这种情况下,它告诉您您传递的参数不是类类型。快速查看错误末尾的参数类型会发现它是一个指针。

【讨论】:

    【解决方案2】:

    QListQImageQString 是 Copy-On-Write 类型(请参阅 other Qt implicitly shared types),因此不应使用指向这些类型的指针,因为它们基本上已经是智能指针。

    如果你从代码中删除所有指针,它也应该解决主要问题。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2016-03-20
      • 2011-07-08
      相关资源
      最近更新 更多