Solved Sample program to pass char array to qtConcurrent
-
Hello Everyone,
Looking for a sample program to pass char array to QTConcurrent.
I have a function which takes char array as an argument and need it pass to the
QtConcurrent.run() function.
I tried various ways of doing the same but no success. -
The problem is not passing it, it's keeping it alive until
run
finishes. Can you useQByteArray
orstd::array
instead of the raw array?EDIT:
This is a bit awkward but it should work:
// void doSomething(char* arr, int n) char myarray[100]; std::fill(std::begin(myarray),std::end(myarray),'A'); /////////////////////////////////////////////////////////////// const int arraySize = std::extent<decltype(myarray)>::value; char* arrayToPass = new char[arraySize]; std::memcpy(arrayToPass,myarray,arraySize); auto runWatcher =new QFutureWatcher<void>(); QObject::connect(runWatcher,&QFutureWatcher::finished,runWatcher,&QFutureWatcher::deleteLater); QObject::connect(runWatcher,&QFutureWatcher::finished,[arrayToPass]()->void{delete [] arrayToPass;}); runWatcher->setFuture(QtConcurrent::run(std::bind(doSomething,arrayToPass,arraySize)));
-
@VRonin : Thanks a lot. The implementation looks quite complex so moving with string instead of char array as you have mentioned in previous thread.