简体   繁体   English

QFuture <void>无效

[英]QFuture<void> won't work

I am trying to make my application run on multiple threads to make its processes more efficient. 我试图让我的应用程序在多个线程上运行,以使其进程更有效。 I found, on the Qt's website, the QFuture temmplate class that could help me. 我在Qt的网站上找到了可以帮助我的QFuture temmplate课程。 I am trying to use like in one of their examples. 我试图在他们的一个例子中使用。 Below is part of my class declaration/definition. 以下是我的类声明/定义的一部分。

class PreferencesWindow {
public:
    PreferencesWindow(QWidget *parent = 0);
public slots:
    void dbsChanged();
}

PreferencesWindow::PreferencesWindow(QWidget *parent = 0) {
    QFuture<void> fns = run(dbsChanged);
}

When I try to run it, I get 48 errors (from this single line) like: 当我尝试运行它时,我得到48个错误(来自这一行),如:

error C2780: 'QFuture<FunctionObject::result_type> QtConcurrent::run(FunctionObject *,const Arg1 &)' : expects 2 arguments - 1 provided

Where am I wrong and how should do this to run that slot on a different thread? 我错在哪里以及如何在不同的线程上运行该插槽?

Why do I want this? 我为什么要这个? The execution of this method could take up to 30 seconds (it checks some database settings). 执行此方法最多可能需要30秒(它会检查一些数据库设置)。 During this time the GUI is frozen, and this will lead to a bad user experience, so I find this to be a good solution. 在此期间,GUI被冻结,这将导致糟糕的用户体验,因此我发现这是一个很好的解决方案。

You should provide the pointer to the object and also the address of the class member function like : 您应该提供指向对象的指针以及类成员函数的地址,如:

QFuture<void> fns = QtConcurrent::run(this,&PreferencesWindow::dbsChanged);

If your function has parameters you can pass them by : 如果您的函数有参数,您可以通过以下方式传递:

QFuture<void> fns = QtConcurrent::run(this,&PreferencesWindow::dbsChanged, val1, val2);

dbsChanged() is a member function - you need to provide an object on which to execute it. dbsChanged()是一个成员函数 - 您需要提供一个执行它的对象。 If you want it on the object itself, use this : 如果你想要的对象本身,使用this

PreferencesWindow::PreferencesWindow(QWidget *parent = 0) {
    QFuture<void> fns = run(dbsChanged, this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM