c++multithreadingqtqfuture

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.


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