如何从qt中的QProcess输出绘图

时间:2017-02-02 07:44:22

标签: qt qprocess

我有C程序,我想在主窗口的Textbrowser中显示该程序的输出,并在小部件中绘图。

#include<stdio.h>
void main()
{
int i,j;
while(1)
{
for(i=0;i<6;i++)
{
     printf("%d\n",i);
     if(i==5)
 {
    for(j=i-1;j>0;j--)
 {
  printf("%d\n",j);
 }
  i=1;
   }
   }
  }
  }
  }
它的出局是     1,     2,     3,     4,     5,     4,     3,     2,     1,     2,     3, 我想在图表中绘制此值。这个C程序通过QProcess从qt访问并存储在Qbyte数组中的输出值。它显示在mainwindow的textbrowser中。但是没有发生QProcess的绘图

我的Qt代码:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
     ui(new Ui::MainWindow)
        {
         ui->setupUi(this);
         QVector<double> data_x(101), data_y(101);
         timer.start();
         init_line_plot();

         x_position = 0;
         process = new QProcess();

        //connect(process, SIGNAL(readyRead()), this, SLOT(receive()));
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(receive()));
connect(process, SIGNAL(readyReadStandardError()), this, SLOT(receive()));
}

 MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow:n_pushButton_clicked()
  {

QProcess process;
 process.start("./test2");
 process.waitForReadyRead();

 QByteArray data =process.readAllStandardOutput();
 qDebug() << data;
 QByteArray err =process.readAllStandardOutput();
 qDebug() << err;
  ui->textEdit->append(QString(data));
 }

void MainWindow::init_line_plot()
{

 ui->customPlot->addGraph();
 ui->customPlot->xAxis->setLabel("t");
 ui->customPlot->yAxis->setLabel("V");

 }
void MainWindow::receive()
{
   // recieves data as ASCII string
 int datalength = 1000;
 char data [1000];
 int bytesRead =process->readLine(data, datalength);
 data[bytesRead]='\0';


  QTextStream out(stdout);
  out << data << endl;
   addDataPoint(atof(data));
  }

 void MainWindow::addDataPoint(double datapoint)
 {
 if (x_position>250)data_x.pop_front();
 double ms = timer.elapsed();
 data_x.push_back((double)ms/1000);
 x_position++;
 if (x_position>250) data_y.pop_front();
 data_y.push_back(datapoint);

ui->customPlot->graph(0)->setData(data_x,data_y);

ui->customPlot->graph(0)->setPen(QPen(QColor(0,200,0)));
ui->customPlot->setBackground(Qt::black);

ui->customPlot->graph(0)->rescaleAxes();
ui->customPlot->replot();

}

我想根据时间图绘制输出值。即... y轴的1,2,3,4和X轴的时间。 代码中需要修改什么?

0 个答案:

没有答案