title: Raspberry pi Qt plotting with high CPU usage
body: <p>I use Qt creator cross-compile on Raspberry pi B+ to develope an App that plot data from file. The program work fine on my laptop but the plot use hi processing on pi B+. when I run the program on my laptop it work with less than 1% of CPU and on pi with 1-2% CPU, when I start the plotting on my Laptop it take about 13% CPU but on pi it take about 95-99% cpu!!!. Plotting on my laptop rich more than 60 frame per second but on pi just 14-16 frame per second and when I overclocked the pi I got about 35 frame per second. my laptop run Ubuntu 14.04 with gnome shell and the CPU is intel centrino core 2 Dou 2 GHz with 4 GB RAM and NVIDIA. This is the source code of my Qt App</p>
  
  <pre><code>    #include "mainwindow.h"
      #include "ui_mainwindow.h"
  
  
      ///*********** STARTING THE CLASS **************************///
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui-&gt;setupUi(this);
  
          MainWindow::plotting();
      //********************* Read File **************************//
          QFile inputFile("ECG_Data.txt");
          if (inputFile.open(QIODevice::ReadOnly))
          {
             QTextStream in(&amp;inputFile);
             int i=0;
             while (!in.atEnd())
             {
                QString line = in.readLine();
                fileData[i]=line.toDouble();
                i++;
             }
             inputFile.close();
          }
  
      }
  
  
      ///**************** START BUTTON *************************///
      void MainWindow::on_pushButton_clicked()
      {
          stop= true;
      }
      ///***************** STOP BUTTON **************************///
      void MainWindow::on_pushButton_2_clicked()
      {
          stop = false;
  
      }
  
      ///**************** PLOTTING *******************************///
      void MainWindow::plotting()
      {
      //***** set plot parameters *******************************//
          ui-&gt;customPlot-&gt;addGraph();
          ui-&gt;customPlot-&gt;graph(0)-&gt;setPen(QPen(Qt::yellow)); //set plot line color
          ui-&gt;customPlot-&gt;xAxis-&gt;setTickLabelType(QCPAxis::ltDateTime);
          ui-&gt;customPlot-&gt;xAxis-&gt;setDateTimeFormat("hh:mm:ss");
          ui-&gt;customPlot-&gt;xAxis-&gt;setAutoTickStep(false);
          ui-&gt;customPlot-&gt;xAxis-&gt;setTickStep(2);
          ui-&gt;customPlot-&gt;axisRect()-&gt;setupFullAxesBox();
  
      //***** set plot background color ****************************//
          QLinearGradient plotGradient;
          plotGradient.setStart(0, 0); //set start point
          plotGradient.setFinalStop(0, 350); //set final point
          plotGradient.setColorAt(1, QColor(100, 100, 100)); // set background color
          ui-&gt;customPlot-&gt;setBackground(plotGradient);
  
      //***** creat connection for plot Axes *************************//
          // make left and bottom axes transfer their ranges to right and top axes:
          connect(ui-&gt;customPlot-&gt;xAxis, SIGNAL(rangeChanged(QCPRange)), ui-&gt;customPlot-&gt;xAxis2, SLOT(setRange(QCPRange)));
          connect(ui-&gt;customPlot-&gt;yAxis, SIGNAL(rangeChanged(QCPRange)), ui-&gt;customPlot-&gt;yAxis2, SLOT(setRange(QCPRange)));
  
  
      //***** creat connection for plot founction *********************//
          // setup a timer that repeatedly calls MainWindow::realtimeDataSlot();
          connect(&amp;dataTimer, SIGNAL(timeout()), this, SLOT(readResponse()));
          dataTimer.start(10); // Interval 0 means to refresh as fast as possible
  
      }
  
  ///**************** REAL TIME PLOTTING SLOT *********************///
      void MainWindow::readResponse()
  
      { if(j==500){j=0;}
  
          double value0 = fileData[j];
          if (stop)
          {j++;
      //***** calculate two new data points: **************************//
          double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0; //first data point of plot from time
          static double lastPointKey = 0;
          if (key-lastPointKey &gt; 0.01) // at most add point every 10 ms
          { //emit plottingData(rx);
          //  double value0 = rxplot; // convert string data of serial port to Double to be the second data point of plot
  
      //***** add data to lines: *****************************//
            ui-&gt;customPlot-&gt;graph(0)-&gt;addData(key, value0);
            // remove data of lines that's outside visible range:
            ui-&gt;customPlot-&gt;graph(0)-&gt;removeDataBefore(key-2);
            ui-&gt;customPlot-&gt;graph(0)-&gt;rescaleValueAxis();
            lastPointKey = key;
          }
  //make key axis range scroll with the data (at a constant range size of 8):
          ui-&gt;customPlot-&gt;xAxis-&gt;setRange(key+0.25, 2, Qt::AlignRight);
          ui-&gt;customPlot-&gt;replot();
  
  //***** calculate frames per second: *********************************//
          static double lastFpsKey;
          static int frameCount;
          ++frameCount;
           if (key-lastFpsKey &gt; 2) // average fps over 2 seconds
           { ui-&gt;statusBar-&gt;showMessage(QString("%1 FPS, Total Data points: %2").arg(frameCount/(key-lastFpsKey), 0, 'f', 0).arg(ui-&gt;customPlot-&gt;graph(0)-&gt;data()-&gt;count()), 0 );
            lastFpsKey = key;
            frameCount = 0;
           }
           }
  
      }
  
      ///****************** DESTRUCTOR ****************************///
      MainWindow::~MainWindow()
      {
          delete ui;
  
      }
  </code></pre>
  
  <p>The App work with full screen on pi "this is the second problem" and don't receive touch event from the touchscreen. I use Qt 5.4 and on pi the operating system is Raspbian Wheezy. I fllowed this instractions to configure the cross-compile on pi <a href="http://exploreembedded.tumblr.com/post/115333857238/guide-to-cross-compile-qt-54-for-the-raspberry-pi" rel="nofollow">here.</a></p>
  
  <p><a href="http://i.stack.imgur.com/wh58q.jpg" rel="nofollow">This is the photo of App window on pi screen</a></p>
  
  <p>please help me :(</p>