Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I would do it:</p> <p>Add a signal to the worker threads that is emitted each time a new batch of data is ready. Connect this signal to a slot defined in the main GUI thread using Qt::QueuedConnection. This slot should collect the processed data from the worker threads and insert it into the table's model. Model should be naturally also in the GUI thread.</p> <h2>Update 1</h2> <p>More detailed explanation. You already got the threads down so all you'll have to do is to expand them a little. Example:</p> <pre><code>// NOTE: // In this example I'm assuming that the thread continues to work after // preparing first batch of data. One thread can prepare multiple batches // over time. If that's not the case for you then you can probably // safely ignore mutex stuff and intermediary tmp variables. class MyWorker : public QThread { // ... public: void run() { while (bWork) { QList&lt;MyData&gt; myData; // Populate 'myData' variable with your data. // ... // Now, store myData in more persistent buffer and emit the // ready signal. dataBatchMutex.lock(); dataBatch.append(myData); dataBatchMutex.unlock(); emit dataBatchReady(); } } QList&lt;MyData&gt; popLastBatch() { QMutexLocker l(&amp;dataBatchMutex); QList&lt;MyData&gt; tmp = dataBatch; dataBatch.clear(); return tmp; } signals: void dataBatchReady(); private: QMutex dataBatchMutex; QList&lt;MyData&gt; dataBatch; }; </code></pre> <p>We have the worker thread. Now about the GUI thread:</p> <ol> <li>Create your worker threads objects.</li> <li>Connect the <code>dataBatchReady()</code> signal to a slot in the GUI thread. Remember to use <code>Qt::QueuedConnection</code>.</li> <li>Start the threads.</li> <li>When that slot is executed you need to figure out which worker thread has the new data. You may pop them all (not very efficient) or you may add additional method to the worker to find out if the <code>dataBatch</code> field is not empty. You may also invent other way to figure out which thread emitted the signal - that's up to you to figure out.</li> <li>When you figure out the thread just call the <code>popLastBatch()</code> on that thread and add the returned list to the model. The model will take care of the rest. If you still have performance issues then refer to tHand's response.</li> </ol> <p><strong>Note:</strong> I didn't test the code so it may contain some obvious errors. You should get the idea, though.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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