Solved Error on QtConcurrent::run
-
What is wrong with these code line:
QtConcurrent::run(positionWidget, &PositionGroupbox::Select, false)
positionWidget is a instance pointer of PositionGroupbox
Select is public void with one bool parameter in the class PositionGroupboxThe compiler shows me an error:
error: no matching function for call to 'run' synchronizer.addFuture(QtConcurrent::run(positionWidget, &PositionGroupbox::Select, false)); ^~~~~~~~~~~~~~~~~
Do you know what is wrong with the code?
-
@peter-70 Perhaps you have to enable concurrent in your PRO file:
QT += concurrent
-
@peter-70 is that a valid overload of run ? If I remember correctly the first paramter has to be a member function not an object.
I may be wrong however.
-
@J.Hilk This usage is totally legal.
For example, from Qt documentation => https://doc.qt.io/qt-5/qtconcurrentrun.html
// call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread QByteArray bytearray = "hello world"; QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ','); ... QList<QByteArray> result = future.result();
-
@peter-70 What is the type of synchronizer? It should be QFutureSynchronizer<void> to match PositionGroupbox::Select() signature.
-
Does
PositionGroupbox
derive fromQWidget
directly or indirectly? -
@KroMignon said in Error on QtConcurrent::run:
QT += concurrent
Good idea, but unfortunately it does not help
-
@peter-70
if you forgotQT += concurrent
inside your *pro file, you have to do a complete rebuild - deleting build folder running qmake - for it to take effect, after adding it!Did you do that?
-
Yes, PositionGroupbox derive from QWidget directly. But What does it matter?
-
@J.Hilk At first, I did not do it. Now I've deleted the output folder and the makefile. Then recreate the makefile and invoke rebuild my project. But that does not help either :-(
-
It should be QFutureSynchronizer<void> to match PositionGroupbox::Select() signature
Yes it is!
Here is the code snippet:
QFutureSynchronizer<void> synchronizer; while ((rc = position.Fetch()) && rowIndex < itemsCount) { auto p = new Position; p->Copy(&position); auto positionWidget = new PositionGroupbox(p, this); p->SetContainer(positionWidget); synchronizer.addFuture(QtConcurrent::run(positionWidget, &PositionGroupbox::Select, false));
-
@peter-70 This sound very strange to me.
I have used QtConcurrent::run() and never run in this error.
I think you have include header:
#include <QtConcurrent/QtConcurrent>
Can you show the declaration of PositionGroupbox::Select?
-
Can you show the declaration of PositionGroupbox::Select?
Yes, the include is as you write it here. But the declaration of PositionGroupbox::Select is the right question and solution!!!
100 points for you! ;-)
The declaration is:
void Select(bool isSelected, bool refreshColors = false);
Now I call
synchronizer.addFuture(QtConcurrent::run(positionWidget, &PositionGroupbox::Select, false, false));
and everything is ok!
@all: Thank you very much! Specially for KroMignon :-)
-
@peter-70 your welcome
-
@peter-70 said in Error on QtConcurrent::run:
Yes, PositionGroupbox derive from QWidget directly. But What does it matter?
It matters quite a lot.
QWidget
is not thread-safe and it's not reentrant. You can't touch anythingQWidget
-y outside the GUI thread, including creating the actual object. Failing to observe this is a race condition and ultimately a bug. That is to say, you have bigger problems than a syntax error.