QtConcurrent::run() is failing sometimes
Solved
QML and Qt Quick
-
Hello All,
I have a requirement of executing a method in thread where the time consuming activity is happens in a thread and emits a signal to QML when the data is available.
After lots of googling, I have found QtConcurrent::run() feature provided by the QML.I have used QtConcurrent::run() feature, but it is failing some times.
modelThread.isRunning() is returning "false" on qDebug().I dont know why, Can anyone please help me on this ?
Below is the pseudo code
Please note, QFuture<void> modelThread is a member variable.
**sampleClass.cpp** sampleClass::sampleClass() { QObject::connect(this, SIGNAL(fillRequest()), this, SLOT(updateRequest())); } ~sampleClass::sampleClass() { modelThread.waitForFinished(); } void sampleClass::sendRequest(QString arg1, int arg2) { //assigning arguments to member variables emit fillRequest(); } void sampleClass::updateRequest() { modelThread = QtConcurrent::run(this, &sampleClass::processRequest); ``` qDebug() << "modelThread = " << modelThread.isRunning(); ``` } void sampleClass::processRequest() { //Time consuming processing happens in this method // emit signal to inform QML that model is ready emit processRequestIsReady(); } In sample.qml, sampleClassObject { id: local_ObjectModel } Component.onCompleted: { local_ObjectModel.sendRequest(arg1, agr2) local_ObjectModel.processRequestIsReady.connect(gridView.sigHandler) } function sigHandler() { gridView.model = local_ObjectModel; }
-
Hi,
As they documentation of the function states:
Runs function in a separate thread. The thread is taken from the global QThreadPool. Note that function may not run immediately; function will only be run once a thread becomes available.