Sample program to pass char array to qtConcurrent
Solved
General and Desktop
-
wrote on 14 Mar 2018, 10:28 last edited by
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. -
wrote on 14 Mar 2018, 10:55 last edited by VRonin
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)));
-
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)));
1/3