Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QtConcurrent::run() is failing sometimes
Forum Updated to NodeBB v4.3 + New Features

QtConcurrent::run() is failing sometimes

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 284 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    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;
        }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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.
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      1
      • SGaistS SGaist

        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.
        
        P Offline
        P Offline
        Praveen.Illa
        wrote on last edited by
        #3

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

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved