0

为了避免在按钮单击时在 GUI 线程中进行计算,我试图在单独的线程中运行它。

只要我想有能力监控进度,我的代码就基于这个源。

所以,我的代码是:

futureWatcher.setFuture(QtConcurrent::mapped(library_pool.cbegin(), library_pool.cend(), util->loadFilesInLibrary(library_pool)));

futureWatcher.waitForFinished();

table = QFuture::result(); 

这与 qt 文档中的给定来源非常相似,但它给出了相应的错误:

C2893: Failed to specialize function template 'QFuture<QtPrivate::MapResultType<void,MapFunctor>::ResultType> QtConcurrent::mapped(Iterator,Iterator,MapFunctor)'
C2780: 'QFuture<QtPrivate::MapResultType<void,MapFunctor>::ResultType> QtConcurrent::mapped(const Sequence &,MapFunctor)': expects 2 arguments - 3 provided
C2955: 'QFuture': use of class template requires template argument list
'QFuture<T>::result': illegal call of non-static member function

那么,如何在button_clicked不制作 GUI 手的情况下使用插槽中非 gui 类的函数正确填充工作线程?

UPD

好吧,我现在已经成功编译了代码,这就是它的工作原理:

futureWatcher.setFuture(QtConcurrent::run(&this->util, &Utils::loadFilesInLibrary, library_pool.at(0)));

futureWatcher.waitForFinished();

library_pool = future.result();

但是按钮单击仍然会导致运行时出错: 运行时错误

我尝试运行的函数不会生成任何小部件,因此此消息的原因尚不清楚。

我已经阅读了几个问题(像这样),所有代码都声明可以工作,但它只存在于main()远离我需要的地方。那么,再次,我如何从 GUI 线程启动它?

4

1 回答 1

0

您可以在包装器的帮助下将非静态成员与 QtConcurrent 方法一起使用。在Qt 的论坛上有更多关于它的讨论。这是另一个类似的问题,询问如何使用包装器。

struct AddWrapper {
  Utils *utils = nullptr;
  typedef MyType result_type;

  AddWrapper(Util *u): instance(u) {};
  MyType operator()(const MyType &t) const {
    return instance->longMethod(t);
  }
}

Utils *foo = new Utils();
AddWrapper wrapper(foo);
QFuture<MyType> results = QtConcurrent::mapped(myList, wrapper);
于 2017-07-18T15:11:35.677 回答