title: Replot function which depends on time, Qt
body: <p>I'm writing a study project and I need to plot a function that depends on real-time. I used <a href="https://www.qcustomplot.com" rel="nofollow noreferrer">QCustomPlot</a>. This plot should start drawing when I pressed a button.
  So I've wrote next code</p>
  <pre class="lang-cpp prettyprint-override"><code>void MainWindow::on_pushButton_restart_clicked()
  {
  ... 
  my_timer-&gt;restart();
  /* Set up and initialize the graph plotting timer */ 
  connect(&amp;timer_plot, SIGNAL(timeout()), this, SLOT(realtimePlot())); // where QTimer timer_plot
  timer_plot.start(20); 
  }
  </code></pre>
  <p><code>realtimePlot()</code> looks like</p>
  <pre class="lang-cpp prettyprint-override"><code>void MainWindow::realtimePlot()
  {
  
      ui-&gt;widget-&gt;graph(0)-&gt;data()-&gt;clear();
  
      double key = my_timer-&gt;elapsed() / 1000.0;
      static double lastPointKey = 0;
  
      if(key - lastPointKey &gt; 0.002)
      {
          for (auto&amp; iter : x) {
              ui-&gt;widget-&gt;graph(0)-&gt;addData(iter, sin(iter+key));
          }
          lastPointKey = key;
      }
      ui-&gt;widget-&gt;replot();
  }
  </code></pre>
  <p>And it works properly, but when I want to restart the plot and pressed the button again there is a delay before the plot will be drawn and it increases with each click on the button, and the animation of the plot becomes less and less smooth. Why is this happening and how can I solve it?</p>
  <p><a href="https://imgur.com/MHiHdr3" rel="nofollow noreferrer">Here</a> you can watch my screencast with the problem.</p>
  <p>Code for real-time plot I took from <a href="https://www.qcustomplot.com/index.php/demos/realtimedatademo" rel="nofollow noreferrer">here</a></p>
  <p>And <code>MainWindow()</code></p>
  <pre class="lang-cpp prettyprint-override"><code>QVector&lt;double&gt; x;
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      ui-&gt;widget-&gt;addGraph();
      ui-&gt;widget-&gt;xAxis-&gt;setRange(0, 20);
      my_timer-&gt;start();
      h = 0.05;
      x.push_back(0);
      for (int i=1; i&lt;=20/h; i++)
      {
          x.push_back(i*h);
      }
  }
  </code></pre>