PDA

View Full Version : QwtPlotCurve->sample() in Qwt 6 & qt 4.7



micc13
17th November 2010, 17:09
I have drawn a curve and I want to get the y-value on the curve corresponding to a given x-value.

I am using a QwtPlot and QwtPlotCurve.

QwtPlotCurve no longer supports

"double y (int i) const ".

Am I correct in thinking that this functionality has been replaced by

"T QwtPlotSeriesItem< T >::sample ( int index ) const" ?

When I try to use this "sample" utility, I get a "Runtime Error" :-

int currentPosX = pos.x();
QPointF d = curve1->sample(currentPosX);
qreal value = d.y();

I have assumed that "sample" returns QPointF because that's the error message I get when I tried it with a double.

Is this the best way to get the curve value?
Why do I get the runtime error?

Any help you can provide will be much appreciated, thanks.

Uwe
18th November 2010, 06:38
When I try to use this "sample" utility, I get a "Runtime Error" :-

int currentPosX = pos.x();
QPointF d = curve1->sample(currentPosX);
qreal value = d.y();


QwtPlotCurve is representing a series of points and "QwtPlotCurve::sample(int index) const" returns the point at a specific index ( like an index in an array ) of this series.

When the x-coordinates of your series corresponds to the indexes your code might be o.k. but in general a x-coordinate is not an index.

But this is not different to Qwt 5.x, beside that you had 2 different methods for retrieving the coordinates of a point.

Uwe

micc13
18th November 2010, 10:04
Thanks Uwe.
Looks like I'll have to do a bit more work to get the value I need.

I'm implementing a vertical selection bar which the user can move to any point along the x-axis.
Wherever the bar is positioned, I want to display the co-ordinates of the point where this vertical bar intersects the curve.

I know the x-value (from pos.x()).
I suppose I will need to use this value to look up my data set and find the points before and after this one, then extrapolate to find the y-value at this point.

By the way, do you think that the runtime error happened because I tried to call "sample" with an x co-ordinate which does not correspond to one of the points used to draw the curve?

Uwe
18th November 2010, 10:28
By the way, do you think that the runtime error happened because I tried to call "sample" with an x co-ordinate which does not correspond to one of the points used to draw the curve?
Normally the x-coordinates have nothing to do with the size of an array. If you have 20 points indexes from 0-19 are valid. A x coordinate of f.e. 1000.0 used as index into this array will be out of bound.

Uwe

micc13
18th November 2010, 13:00
Ah, I get it now, thanks again Uwe.

I am now using the following solution, which works :-

cursorPosition= ui->analysisQwtPlot->invTransform(QwtPlot::xBottom, pos.x());
QPointF pointOnCurve1 = curve1->sample(round(cursorPosition)-1);
qreal value1 = pointOnCurve1.y();
ui->positionLabel1->setText(tr("Curve 1 Time = %1 seconds, Value = %2 units").arg(round(cursorPosition)).arg(value1, 0, 'f', 2));