Hello,

i use in my QwtPlot a curve tracker like in the playground example "CurveTracker".

Qt Code:
  1. #include "curvetracker.h"
  2. #include <qwt_picker_machine.h>
  3. #include <qwt_series_data.h>
  4. #include <qwt_plot.h>
  5. #include <qwt_plot_curve.h>
  6.  
  7. struct compareX
  8. {
  9. inline bool operator()( const double x, const QPointF &pos ) const
  10. {
  11. return ( x < pos.x() );
  12. }
  13. };
  14.  
  15. CurveTracker::CurveTracker( QWidget *canvas ):
  16. QwtPlotPicker( canvas )
  17. {
  18. setTrackerMode( QwtPlotPicker::ActiveOnly );
  19. setRubberBand( VLineRubberBand );
  20.  
  21. setStateMachine( new QwtPickerDragPointMachine() );
  22. }
  23.  
  24. QRect CurveTracker::trackerRect( const QFont &font ) const
  25. {
  26. QRect r = QwtPlotPicker::trackerRect( font );
  27.  
  28. // align r to the first curve
  29.  
  30. const QwtPlotItemList curves = plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
  31. if ( curves.size() > 0 )
  32. {
  33. QPointF pos = invTransform( trackerPosition() );
  34.  
  35. const QLineF line = curveLineAt(
  36. static_cast<const QwtPlotCurve *>( curves[0] ), pos.x() );
  37. if ( !line.isNull() )
  38. {
  39. const double curveY = line.pointAt(
  40. ( pos.x() - line.p1().x() ) / line.dx() ).y();
  41.  
  42. pos.setY( curveY );
  43. pos = transform( pos );
  44.  
  45. r.moveBottom( pos.y() );
  46. }
  47. }
  48.  
  49. return r;
  50. }
  51.  
  52. QwtText CurveTracker::trackerTextF( const QPointF &pos ) const
  53. {
  54. QwtText trackerText;
  55.  
  56. trackerText.setColor( Qt::black );
  57.  
  58. QColor c( "#333333" );
  59. trackerText.setBorderPen( QPen( c, 2 ) );
  60. c.setAlpha( 200 );
  61. trackerText.setBackgroundBrush( c );
  62.  
  63. QString info;
  64.  
  65. const QwtPlotItemList curves =
  66. plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
  67.  
  68. for ( int i = 0; i < curves.size(); i++ )
  69. {
  70. const QString curveInfo = curveInfoAt(
  71. static_cast<const QwtPlotCurve *>( curves[i] ), pos );
  72.  
  73. if ( !curveInfo.isEmpty() )
  74. {
  75. if ( !info.isEmpty() )
  76. info += "<br>";
  77.  
  78. info += curveInfo;
  79. }
  80. }
  81.  
  82. trackerText.setText( info );
  83. return trackerText;
  84. }
  85.  
  86. QString CurveTracker::curveInfoAt(
  87. const QwtPlotCurve *curve, const QPointF &pos ) const
  88. {
  89. const QLineF line = curveLineAt( curve, pos.x() );
  90. if ( line.isNull() )
  91. return QString::null;
  92.  
  93. const double y = line.pointAt(
  94. ( pos.x() - line.p1().x() ) / line.dx() ).y();
  95.  
  96. QString info( "<font color=""%1"">%2</font>" );
  97. return info.arg( curve->pen().color().name() ).arg( y );
  98. }
  99.  
  100. QLineF CurveTracker::curveLineAt(
  101. const QwtPlotCurve *curve, double x ) const
  102. {
  103. QLineF line;
  104.  
  105. if ( curve->dataSize() >= 2 )
  106. {
  107. const QRectF br = curve->boundingRect();
  108. if ( ( br.width() > 0 ) && ( x >= br.left() ) && ( x <= br.right() ) )
  109. {
  110. int index = qwtUpperSampleIndex<QPointF>(
  111. *curve->data(), x, compareX() );
  112.  
  113. if ( index == -1 &&
  114. x == curve->sample( curve->dataSize() - 1 ).x() )
  115. {
  116. // the last sample is excluded from qwtUpperSampleIndex
  117. index = curve->dataSize() - 1;
  118. }
  119.  
  120. if ( index > 0 )
  121. {
  122. line.setP1( curve->sample( index - 1 ) );
  123. line.setP2( curve->sample( index ) );
  124. }
  125. }
  126. }
  127.  
  128. return line;
  129. }
To copy to clipboard, switch view to plain text mode 
This works and i get always the values from the actual Mouse Position.

Now i want to double Click at a selected position and want the tracker to stay visible also when i move the mouse outside the plot area.

How can i manage this?

Thanks for help

DSP