Hello
I once implemented following method called by the trackerText() method. May be that helps?
bool PlotPicker
::curveSelected(const QPoint pMousePos, PlotCurve
* &pPlotCurve,
double &pYValue
) const {
double lDist = 10e10;
int lIndex = -1;
// get nearest curve point by iterating over plot items, filtering for curves and calculating minimum distance
const QwtPlotItemList& lPlotItemList = plot()->itemList();
for (QwtPlotItemIterator lPlotItemIterator = lPlotItemList.begin(); lPlotItemIterator != lPlotItemList.end(); ++lPlotItemIterator)
{
if ((*lPlotItemIterator
)->rtti
() == QwtPlotItem::Rtti_PlotCurve) {
PlotCurve *lTmpCurve = (PlotCurve*)(*lPlotItemIterator);
double lTmpDist;
int lTmpIndex = lTmpCurve->closestPoint(pMousePos, &lTmpDist);
if (lTmpDist < lDist && lTmpIndex > -1)
{
pPlotCurve = lTmpCurve;
lDist = lTmpDist;
lIndex = lTmpIndex;
}
}
}
// check if mouse position is in tolerance
if (pPlotCurve && lDist < _pix_tolerance_)
{
pYValue = pPlotCurve->y(lIndex);
return true;
}
pPlotCurve = 0;
return false;
}
bool PlotPicker::curveSelected(const QPoint pMousePos, PlotCurve* &pPlotCurve, double &pYValue) const
{
double lDist = 10e10;
int lIndex = -1;
// get nearest curve point by iterating over plot items, filtering for curves and calculating minimum distance
const QwtPlotItemList& lPlotItemList = plot()->itemList();
for (QwtPlotItemIterator lPlotItemIterator = lPlotItemList.begin(); lPlotItemIterator != lPlotItemList.end(); ++lPlotItemIterator)
{
if ((*lPlotItemIterator)->rtti() == QwtPlotItem::Rtti_PlotCurve)
{
PlotCurve *lTmpCurve = (PlotCurve*)(*lPlotItemIterator);
double lTmpDist;
int lTmpIndex = lTmpCurve->closestPoint(pMousePos, &lTmpDist);
if (lTmpDist < lDist && lTmpIndex > -1)
{
pPlotCurve = lTmpCurve;
lDist = lTmpDist;
lIndex = lTmpIndex;
}
}
}
// check if mouse position is in tolerance
if (pPlotCurve && lDist < _pix_tolerance_)
{
pYValue = pPlotCurve->y(lIndex);
return true;
}
pPlotCurve = 0;
return false;
}
To copy to clipboard, switch view to plain text mode
Within the trackerText() the call is..
//..... some preparations
// check if tracker text shall be displayed
if(mTrackerValueEnable)
{
double lSelectedYValue = 0.0;
PlotCurve* lSelectedCurve = 0;
if (curveSelected(pMousePos, lSelectedCurve, lSelectedYValue))
{
//..... whatever needed
}
}
QColor lBackground
(Qt
::lightGray);
lBackground.setAlpha(_text_background_alpha_);
lText.
setBackgroundBrush(QBrush(lBackground
));
return lText;
//..... some preparations
// check if tracker text shall be displayed
if(mTrackerValueEnable)
{
double lSelectedYValue = 0.0;
PlotCurve* lSelectedCurve = 0;
if (curveSelected(pMousePos, lSelectedCurve, lSelectedYValue))
{
lLabel.append(QString("\n%1").arg(QString::number(lSelectedYValue)));
//..... whatever needed
}
}
QwtText lText(lLabel);
QColor lBackground(Qt::lightGray);
lBackground.setAlpha(_text_background_alpha_);
lText.setBackgroundBrush(QBrush(lBackground));
return lText;
To copy to clipboard, switch view to plain text mode
Regards
Stefan
Bookmarks