Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to listen for a "finished" indication after moving the execution to another thread in Qt
    primarykey
    data
    text
    <p>I have a non-GUI operation which I intend to perform in a worker thread. After some googling, I figured out a simple way to achieve this <a href="http://qt-project.org/wiki/Threads_Events_QObjects" rel="nofollow">here</a></p> <p>In particular, the <code>QMetaObject::invokeMethod()</code> suits me fine. I have also taken care of not sub-classing QThread and instead subclassing QObject to create my worker object and then moving the thread affinity of this object to a newly created thread. (decribed in some detail <a href="http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/" rel="nofollow">here</a></p> <p>Now my question:</p> <p>I will begin queuing methods from my main thread (I imagine this happens under the hood using a QEventLoop, even though I have not set one up and even though I have not re-implemented <code>QThread::run()</code> or called <code>QThread::exec()</code>) I need to find out when my worker has finished processing. In other words, I need to know when the QEventLoop is empty. I thought I could use the signal finished() for this. But it does not appear to work. Can someone throw some light on how to find out when a worker thread has finished execution? If you need more info/code, just let me know. </p> <p>Class Plugin is a subclass of QObject</p> <pre><code>Plugin::Plugin() { m_workerThread = new QThread(this); m_workerThread-&gt;start(QThread::IdlePriority); m_worker = new DataWorker(this); connect(m_workerThread, SIGNAL(finished()), m_worker, SLOT(deleteLater())); m_worker-&gt;moveToThread(m_workerThread); } </code></pre> <p>...</p> <pre><code>Plugin::updateData() { .... if( true == QMetaObject::invokeMethod(m_worker, "RunFProcToGetData", Qt::QueuedConnection, Q_ARG(QString, foo))) { qDebug() &lt;&lt; "successfully invoked RunFProcToGetData"; } if( true == QMetaObject::invokeMethod(m_worker, "updateDataIntoDB", Qt::QueuedConnection, Q_ARG(QSqlDatabase, db), Q_ARG(QString, foo))) { qDebug() &lt;&lt; "successfully invoked updateDataIntoDB"; } .... } </code></pre> <p>In Dataworker class</p> <pre><code>Dataworker::RunProcToGetData(const QString &amp;foo) { // invoke a program to get the Data } Dataworker::updateDataIntoDB(const QSqlDatabase&amp; db, const QString &amp;foo) { // Update database } </code></pre> <p>So when there are no more updateDataIntoDB and RunFProcToGetData left to process, I need a trigger</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload