PDA

View Full Version : Tool tip in point of curve



ruzik
5th October 2011, 18:18
Hello!
I have curve built by 100 points, how can i add tool tip with x and y to this points
I try to use qwt symbol for this goal, but there is not method setToolTip
In advance many thanks for your help!

pkj
6th October 2011, 07:05
You can use QwtPlotPicker with QwtPicker::AlwaysOn to display text as you traverse the graph. See examples too.

ruzik
12th October 2011, 18:24
i need to see coordinates not all the time, i need to see it if mouse moved near some point
I know, that i can redefine mouse move event in qwt symbolm but there no more easy way?

Uwe
13th October 2011, 14:03
i need to see coordinates not all the time, i need to see it if mouse moved near some point
I know, that i can redefine mouse move event in qwt symbolm but there no more easy way?
No you can't - QwtSymbol is no QWidget.

Instead you can follow the hint of pkj using a customized picker:


class YourPlotPicker:: public QwtPlotPicker
{
....

virtual QwtText trackerTextF( const QPointF &pos ) const
{
QwtPlotCurve *curve = NULL;
double dist = 10e10;
int index = -1;

const QwtPlotItemList& itmList = plot()->itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
if ( ( *it )->rtti() == QwtPlotItem::Rtti_PlotCurve )
{
QwtPlotCurve *c = static_cast<QwtPlotCurve *>( *it );

double d;
int idx = c->closestPoint( pos, &d );
if ( d < dist )
{
curve = c;
index = idx;
dist = d;
}
}
}

QString text;

const int maxPixels = 10;
if ( dist < maxPixels )
{
const QPointF pos = curve->sample( index );
text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
}

return QwtText( text );
}
};

QwtPlotCurve::closestPoint() iterates over all points what might be too slow for many points. If can implement something faster using the characteristics of your samples ( f.e when they have increasing x values ) better implement it. In the worst case you need to introduce some spatial organized index like a quadtree.

Uwe

ruzik
23rd October 2011, 12:23
Thank you for your help!

Added after 50 minutes:

Oh, function clossestPoint() dont work right!
//http://hostingkartinok.com/image/01201110/54dfa7b8bb51489265f5c2141a17a7df.jpg
For example look at this
http://hostingkartinok.com/image/01201110/a5814385adf781704321ba947e6bc613.jpg
I checked: in the higher point distance is bigger, even if the cursor directly on the chart