PDA

View Full Version : error: no matching function for call to QtConcurrent::run()



been_1990
4th December 2013, 19:19
error: no matching function for call to 'run(Widget* const, bool (Widget::*)(bool))'
QFuture<void> future = QtConcurrent::run(this, &Widget::loadMenuItems);
^

Defined in header as:

bool loadMenuItems(bool reload=false);

Widget.cpp:


Widget::Widget(QWidget *parent) :
QLineEdit(parent)
{

QFuture<void> future = QtConcurrent::run(this, &Widget::loadMenuItems);
future.waitForFinished();

}


I am following the example in the docs, but I keep getting this error.

ChrisW67
5th December 2013, 00:02
Your loadMenuItems() is a free function not a member of the Widget class, so the second argument makes no sense.
If you remove the "&Widget::" from the second argument then the first argument makes no sense.
You are telling QConcurrent::run() that the function returns void when the declaration indicates it returns bool.

Why you are using a thread here is also not clear, especially since you then block waiting for it to finish. Why not just call loadMenuItems() directly?

been_1990
5th December 2013, 02:33
I forgot to mention that loadMenuItems() is a Widget private method.
I get the same error if I do
QFuture<bool> future = QtConcurrent::run(this, &Widget::loadMenuItems);
And the blocking part is just what came with the example, I intend to remove it.

ChrisW67
5th December 2013, 02:54
I forgot to mention that loadMenuItems() is a Widget private method.

Not if it is defined like your example.

QtConcurrent does not know about your default parameter, just that the signature is "bool f(bool)" so you must provide a value in your call to QtConcurrent::run(). This, for example, compiles:


class Widget: public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *p = 0): QWidget(p) {
QFuture<bool> future = QtConcurrent::run(this, &Widget::loadMenuItems, true);
}
private:
bool loadMenuItems(bool flag = false) {
return flag;
}
};



And the blocking part is just what came with the example, I intend to remove it.
If you do not block at that point then the variable future will go out of scope immediately and you will no have access to the state or return of the thread.

Your loadMenuItems() function will run in another thread. Be careful if you try to access the parent object, allocate memory you expect to use in the GUI thread, or do things that are only supported in the GUI thread.

been_1990
5th December 2013, 04:10
Ah, I thought it knew about the default value for the parameter, that fixes it.
My use case is that i have to load many items from a list stored on file, I imagine I can read the file into a QList and then pass it back to the main thread by emitting a signal?

ChrisW67
5th December 2013, 04:37
Yes, I guess you can.