1

I have a recursive directory copy function I'd like to run in the background. The function takes two QString arguments, filepath and dir.

From .pro:

QT += core gui sql network concurrent
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
LIBS += -lQt5Concurrent

From code:

#include <QtConcurrent/QtConcurrent>
#include <qtconcurrentrun.h>
#include <QThread>
...

QFuture <void> future = QtConcurrent::run(copyFolder(filepath,dir));

I get the following compile error:

 error: no matching function for call to 'run'
 QFuture <void> future = QtConcurrent::run(copyFolder(filepath,dir) );
                  ^~~~~~~~~~~~~~~~~

If I call the function like this:

QFuture <void> future = QtConcurrent::run(copyFolder, filepath,dir );

error: reference to non-static member function must be called
QFuture <void> future = QtConcurrent::run(copyFolder, filepath,dir );
                            ^~~~~~~~~~

What am I doing wrong?

demonplus
  • 5,613
  • 12
  • 49
  • 68
jocala
  • 31
  • 2
  • 6
  • 1
    Are there any other lines below the error? E.g. "note: candidates are:" and so on? – Oleg Andriyanov Oct 06 '16 at 21:19
  • Yes. Quite a few. – jocala Oct 06 '16 at 21:45
  • After the edit, You seem to be trying to call another member function using `QtConcurrent::run`, see my answer. – Mike Oct 06 '16 at 22:04
  • 1
    The `LIBS += -lQt5Concurrent` is wrong. The relevant libraries are automatically linked with when you provide `QT += concurrent`. You also don't need the `core` module. And since your code likely doesn't support Qt 4 anyway, you can get rid of the `greaterThan` test. Then the four lines of the `.pro` file that you show above reduce to `QT += widgets concurrent network sql` – Kuba hasn't forgotten Monica Oct 07 '16 at 02:09

1 Answers1

4

I get the following compile error:

error: no matching function for call to 'run'
QFuture <void> future = QtConcurrent::run(copyFolder(filepath,dir) );
                  ^~~~~~~~~~~~~~~~~

You shouldn't be passing arguments the way you are doing. Instead you should pass your function's argument as arguments to QtConcurrent::run, see docs here.

Your call should look something like this:

QFuture <void> future = QtConcurrent::run(copyFolder, filepath, dir);

This way QtConcurrent will copy the arguments you have passed and perform the function call in a thread from the default QThreadPool .

If I call the function like this:

QFuture <void> future = QtConcurrent::run(copyFolder, filepath,dir );
  error: reference to non-static member function must be called
QFuture <void> future = QtConcurrent::run(copyFolder, filepath,dir );
                               ^~~~~~~~~~

If you have to call a non-static member function in your class, you have to pass the object that you want to run the function on, see docs here. Assuming you are using QtConcurrent::run from another member function in your class (since you would get a different error if not), you should do something like this:

QFuture <void> future = QtConcurrent::run(this, &ClassName::copyFolder, filepath, dir);

Note that, you have to make sure that that all accesses to shared data (between both threads) should be serialized (maybe using a QMutex).

Mike
  • 8,055
  • 1
  • 30
  • 44