Qt Slots called too frequently
<p>I have a Worker Thread that copes with heavy and long computations (up to tenth of seconds). These computations produce several thousands of <code>QLine</code>s, representing the edges of a dynamically-growing tree.
These edges can be modified anytime, since they connect the nodes of the trees by checking the cost, represented by the distance.
I would like a smooth update of the QGraphicsScene containing the edges.
I tried with signal and slots:</p>

<ul>
<li>Worker thread emits a signal, so when the buffer is full this signal gets caught by the main thread, that will cope with the update/drawing of the line</li>
<li>This signal gets still caught by the main thread, but it seems it gets emitted very often, so <code>QGraphicsView</code> gets choked with <code>QLine</code> to be added</li>
<li>Changing the size of the buffer doesn't matter</li>
<li>Is there an alternative approach to this?</li>
</ul>

<p>The main slot is:</p>

<pre><code>void MainWindow::update_scene(bufferType buffer)
{
  for (int i = 0; i &lt; buffer.size(); ++i)
  {
      if (buffer[i].first &lt; (g_edges.size() - 1))
      {
          delete g_edges[buffer[i].first];
          g_edges[buffer[i].first] = scene-&gt;addLine(buffer[i].second);
      }
      else
          g_edges.push_back(scene-&gt;addLine(buffer[i].second));
   }
}
</code></pre>

<p>Note that <code>bufferType</code> is of type <code>QList&lt;std::pair&lt;int,QLine&gt;&gt;</code>.
Here is the heavy computing part</p>

<pre><code>while (T.size() &lt; max_nodes_number &amp;&amp; !_stop)
{
    const cnode random_node = rand_conf ();
    const cnode nearest_node = T.nearest_node (random_node);
    cnode new_node = new_conf (nearest_node, random_node);

    if (obstacle_free(nearest_node, new_node))
    {
        QList&lt;cnode*&gt; X_near = T.neighbours (new_node, max_neighbour_radius);
        cnode lowest_cost_node = nearest_node;
        qreal c_min = nearest_node.cost() + T.distance (nearest_node, new_node);

        for (int j = 0; j &lt; X_near.size(); ++j)
        {
            if (obstacle_free(*X_near[j], new_node) &amp;&amp; ((X_near[j]-&gt;cost() + T.distance (*X_near[j], new_node)) &lt; c_min))
            {
                c_min = X_near[j]-&gt;cost() + T.distance (*X_near[j], new_node);
                lowest_cost_node = *X_near[j];
            }
        }

        T.add_node (new_node, lowest_cost_node.id());
        queue (new_node.id(), QLine (new_node.x(), new_node.y(), lowest_cost_node.x(), lowest_cost_node.y()));

        for (int j = 0; j &lt; X_near.size(); ++j)
        {
            if (obstacle_free(*X_near[j], new_node) &amp;&amp; (new_node.cost() + T.distance (new_node, *X_near[j])) &lt; X_near[j]-&gt;cost())
            {
                queue (X_near[j]-&gt;id(), QLine (new_node.x(), new_node.y(), X_near[j]-&gt;x(), X_near[j]-&gt;y()));

                T.update_parent (*X_near[j], new_node.id());
                T.rewire_tree (X_near[j]-&gt;id());
            }
        }
    }
}
emit finished();
</code></pre>

<p>Please note that <code>T</code> is a class representing a Tree. It is constituted by some methods allowing to add a node, searching for the nearest one, etc. It has a <code>QList&lt;cnode&gt;</code> as private member, storing the tree's nodes. <code>cnode</code> is a structure constituted of two coordinates, an id, a parent, a cost, a list of its children.</p>