简体   繁体   中英

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. 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:

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). 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.

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. If you want it on the object itself, use this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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