在Qt中添加一个幻灯片,在y轴上上下移动信号

时间:2016-06-11 17:54:52

标签: qt qcustomplot qslider

我在同一个图表上有三个实时信号,但有时它们重叠,我需要一个滑动按钮来在y轴上下移动它们以更好地看到它们。如何将幻灯片连接到图表?就像幻灯片的值发生变化一样,信号的数据会在real_y_values + slide_value的图表上添加?这是mainwindow.cpp:

MainWindow::MainWindow(QSerialPort* s,QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
reader(s)
{
  ui->setupUi(this);
  connect(ui->verticalSlider,SIGNAL(valueChanged(int)),ui->customPlot,SLOT(deplasare()));

  setGeometry(400, 250, 542, 390);
  grafic(ui->customPlot);
  setWindowTitle("Real Time Data Graph for EDA ");
  statusBar()->clearMessage();
  ui->customPlot->replot();

}

void MainWindow::grafic(QCustomPlot *customPlot)
{
 Graph_Name = "Real Time Data Graph for EDA";
 customPlot->addGraph(); // blue line
 customPlot->graph(0)->setPen(QPen(Qt::blue));
 customPlot->addGraph();
 customPlot->graph(1)->setPen(QPen(Qt::blue));

 customPlot->addGraph(); // red line
 customPlot->graph(2)->setPen(QPen(Qt::red));
 customPlot->addGraph();
 customPlot->graph(3)->setPen(QPen(Qt::red));

 customPlot->addGraph(); // green line
 customPlot->graph(4)->setPen(QPen(Qt::green));

 customPlot->axisRect()->setupFullAxesBox();

 connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
 dataTimer.start(100); // Interval 0 means to refresh as fast as possible
 }

 void MainWindow::realtimeDataSlot()
 {
 timeCounter+=10;
QByteArray data1;
data1=reader._read_callback();
int sz = data1.size();
int value0;
int value2=800;
int ssz=0;
for(int ii=0;ii<sz;ii++)
   if((int)data1[ii] != 13 && (int)data1[ii] != 10)
    {
        value0=(int)data1[ii];
        ssz++;
        //fct add graph
         ui->customPlot->graph(0)->addData(timeCounter, value0);
         buf.push(value0);
         ui->customPlot->graph(2)->addData(timeCounter, buf.get_SCL());
         cout<<value0<<"   "<<buf.get_SCL()<<endl;
     }




if(timeCounter>=800)
 {
      timeCounter = 0;

      ui->customPlot->graph(1)->clearData();
      ui->customPlot->graph(1)->addData(*(ui->customPlot->graph(0)->data()));
      ui->customPlot->graph(0)->clearData();

      ui->customPlot->graph(3)->clearData();
      ui->customPlot->graph(3)->addData(*(ui->customPlot->graph(2)->data()));
      ui->customPlot->graph(2)->clearData();
 }

 else {


    ui->customPlot->graph(4)->addData(timeCounter, value2);

    ui->customPlot->xAxis->setRange(0,800);
    ui->customPlot->yAxis->setRange(-300, 1024);
  }
    ui->customPlot->graph(1)->removeData(timeCounter, timeCounter+50);
    ui->customPlot->graph(3)->removeData(timeCounter, timeCounter+50);

    ui->customPlot->replot();

}
    void MainWindow::deplasare()
     {

       }

    MainWindow::~MainWindow(){

      delete ui;
     }

我在MainWindow中创建了一个插槽:void MainWindow::deplasare()以便将幻灯片与信号连接起来,但我无法弄清楚这个功能的内容。

1 个答案:

答案 0 :(得分:0)

首先,请查看this以了解如何使用QSlider 从那里,您应该了解您需要将connect更改为:

connect(ui->verticalSlider,SIGNAL(valueChanged(int)),this,SLOT(deplasare(int)));

现在,如果要对某个图形进行某种偏移,则必须将此偏移量添加到所有数据点和重新绘制。以下是在ui->customPlot->graph(0)上执行此操作的示例:

void MainWindow::deplasare(int offset){
    QCPDataMap *dataMap = ui->customPlot->graph(0)->data();
    for (QMap<double,QCPData>::iterator it = dataMap->begin(); it != dataMap->end(); ++it){
        it.value().value += offset;
    }
    ui->customPlot->replot();
}

我们在上面看到的一些解释:
由于QCPGraph数据在QCPDataMap中保留,实际上是QMap<double,QCPData>。为了将offset添加到图表中的每个数据点,我们会迭代QMap并将offset添加到每个QCPData::value