Creating a new thread with QtConcurrent did not work
-
Hello,
I have a function that should run in another thread than the main/GUI-Thread. For this I want to use
QtConcurrent::run()
.
As descriped here it looks like a super simple thing.
But when I implement this I get the compiler warning:
No instance of overloaded function matches the argument list.
//Testclass.cpp TestClass::TestClass() { QFuture<void> future = QtConcurrent::run(TestFunction); } void TestClass::TestFunction() { qDebug() << "Do nothing."; }
QtConcurrent is included in the .pro file and in the header file of the class (also QFuture).
I think there is a problem with the void type. But why? -
It's not a warning but an error.
Is TestClass::TestFunction() a static function? Otherwise it will not work since QtConcurrent::run() needs a pointer to a function, not a pointer to a class function (because it's not resolvable - c++ basics). Or pass a lambda to QtConcurrent::run() -
@Christian-Ehrlicher
Thank you! I tried Lambda expression and it works.
Do you know if there there is a deeper tutorial or info material for the Concurrent module as the offical docs? I did not find something. -
@makopo said in Creating a new thread with QtConcurrent did not work:
I did not find something.
Have you read this part of documentation:
- For Qt 5: https://doc.qt.io/qt-5/qtconcurrentrun.html
- For Qt 6 (some additionnal tools like
QPromise
): https://doc.qt.io/qt-6/qtconcurrentrun.html
I think there is all you need to understand how to use
QtConcurrent::run()
-
@makopo said in Creating a new thread with QtConcurrent did not work:
Do you know if there there is a deeper tutorial or info material for the Concurrent module as the offical docs? I did not find something.
You have to read the section about member functions in the official docs. Member functions always have an implicit
this
as parameter. The least you have to do is to specify the object on which to call the member function. -
Yes, its my mistake. I read over this section in the docs und had searched for other sources... :-/