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:
Q_OBJECT
public:
QFuture<bool> future = QtConcurrent::run(this, &Widget::loadMenuItems, true);
}
private:
bool loadMenuItems(bool flag = false) {
return flag;
}
};
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;
}
};
To copy to clipboard, switch view to plain text mode
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.
Bookmarks