简体   繁体   中英

Accessing the data in 2D QVector using Qt

Using sources online I have managed to live plot 4 graphs of sensor values over time, however I want to use the values from the sensors to plot a live plot a 5th plot using the values from the previous plots. The variable sampleVector is a 2D array containing sensory values (Y-variable) over time (X-variable)

The formula for the 5th plot is: Plot5= -0.5 * (Plot0 + Plot1) ... over all the samples

Below is my attempt at achieving this, however it did not work. The 5th plot seemed to plot twice as fast like it was appending twice as many x-values. It also did not seem to like me multiplying a value such as "-0.5". I am new to Qt and I haven't found a way to manipulate and use the Y-values of "sampleVector". I don't quite understand the format of the variable either if that can be explained as well please.

Thank you in advance

void Plot::plotSampleVector(QVector<QVector<QPointF> > sampleVector){

//*** length of the data
 const int sampleSize = sampleVector.length();

//***Append new values to "QVector<Plot *> d_plots"
 for (int ii=0; ii< sampleSize; i++){

    d_plots[0] -> AppendPoint(sampleVector.at(ii).at(0));
    d_plots[1] -> AppendPoint(sampleVector.at(ii).at(1));
    d_plots[2] -> AppendPoint(sampleVector.at(ii).at(2));
    d_plots[3] -> AppendPoint(sampleVector.at(ii).at(3));
//***Problem Code:
    d_plots[4] -> AppendPoint(-0.5 * (sampleVector.at(ii).at(0) + sampleVector.at(ii).at(1)));

  }

//***Draw Curves
  for (int ii=0; ii<5; ii++){
    d_plots[ii] ->DrawCurveSegment(sampleSize)
   }

}

You are manipulating QPointF which behaves like a mathematical point. So when you do

-0.5 * (sampleVector.at(ii).at(0) + sampleVector.at(ii).at(1))

You are creating a new point with :

x = -0.5 * (x_0 + x_1)
y = -0.5 * (y_0 + y_1)

But from your question I think what you want is:

x = x_0 = x_1
y = -0.5 * (y_0 + y_1)

Which could be written (if the x-values are the same):

QPointF(sampleVector.at(ii).at(0).x(), -0.5 * (sampleVector.at(ii).at(0).y() + sampleVector.at(ii).at(1).y()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM