Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtConcurrent::run() is failing sometimes



  • 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;
        }
    

  • Lifetime Qt Champion

    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.
    


  • @SGaist - Thanks for the information. The issue is resolved now. Below is the sample code

    QFutureWatcher<void> *modelThread = setFuture(QtConcurrent::run(this, &classname::method));


Log in to reply