Hello,
I'm using the class "curvetracker" from the Qwt playground with some minor modifications:
#include "curvetracker.h"
#include <qwt_picker_machine.h>
#include <qwt_series_data.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
struct compareX
{
inline bool operator
()( const double x,
const QPointF &pos
) const {
return ( x < pos.x() );
}
};
CurveTracker
::CurveTracker( QWidget *canvas
):{
setRubberBand( VLineRubberBand );
}
QRect CurveTracker
::trackerRect( const QFont &font
) const {
// Align the rect to the first curve
const QwtPlotItemList curves
= plot
()->itemList
( QwtPlotItem::Rtti_PlotCurve );
if ( curves.size() > 0 )
{
QPointF pos
= invTransform
( trackerPosition
() );
const QLineF line
= curveLineAt
( if ( !line.isNull() )
{
const double curveY = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
pos.setY( curveY );
pos = transform( pos );
r.moveBottom( pos.y() );
}
}
return r;
}
{
trackerText.setColor( Qt::white );
// Background color of the rectangle
//QColor c( "#5e5e5e" );
trackerText.
setBorderPen( QPen( c,
2 ) );
c.setAlpha( 200 );
trackerText.setBackgroundBrush( c );
const QwtPlotItemList curves =
for ( int i = 0; i < 1; i++ )
{
const QString curveInfo
= curveInfoAt
(
if ( !curveInfo.isEmpty() )
{
if ( !info.isEmpty() )
info += "<br>";
info += curveInfo;
}
}
trackerText.setText( info );
return trackerText;
}
{
const QLineF line
= curveLineAt
( curve, pos.
x() );
if ( line.isNull() )
int x = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).x();
double y = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
// Info "window" args: color, x and y.
QString info
( "<font color=""%1"">x : %2<br>y : %3</font>" );
return info.
arg( "white" ).
arg( x
).
arg( QString::number(y,
'f',
3) );
// curve->pen().color().name() }
QLineF CurveTracker
::curveLineAt( {
if ( curve->dataSize() >= 2 )
{
const QRectF br
= curve
->boundingRect
();
if ( br.isValid() && x >= br.left() && x <= br.right() )
{
int index = qwtUpperSampleIndex<QPointF>(
*curve->data(), x, compareX() );
if ( index == -1 &&
x == curve->sample( curve->dataSize() - 1 ).x() )
{
// the last sample is excluded from qwtUpperSampleIndex
index = curve->dataSize() - 1;
}
if ( index > 0 )
{
line.setP1( curve->sample( index - 1 ) );
line.setP2( curve->sample( index ) );
}
}
}
return line;
}
#include "curvetracker.h"
#include <qwt_picker_machine.h>
#include <qwt_series_data.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
struct compareX
{
inline bool operator()( const double x, const QPointF &pos ) const
{
return ( x < pos.x() );
}
};
CurveTracker::CurveTracker( QWidget *canvas ):
QwtPlotPicker( canvas )
{
setTrackerMode( QwtPlotPicker::ActiveOnly );
setRubberBand( VLineRubberBand );
setStateMachine( new QwtPickerDragPointMachine() );
}
QRect CurveTracker::trackerRect( const QFont &font ) const
{
QRect r = QwtPlotPicker::trackerRect( font );
// Align the rect to the first curve
const QwtPlotItemList curves = plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
if ( curves.size() > 0 )
{
QPointF pos = invTransform( trackerPosition() );
const QLineF line = curveLineAt(
static_cast<const QwtPlotCurve *>( curves[0] ), pos.x() );
if ( !line.isNull() )
{
const double curveY = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
pos.setY( curveY );
pos = transform( pos );
r.moveBottom( pos.y() );
}
}
return r;
}
QwtText CurveTracker::trackerTextF( const QPointF &pos ) const
{
QwtText trackerText;
trackerText.setColor( Qt::white );
// Background color of the rectangle
//QColor c( "#5e5e5e" );
QColor c( "#858585" );
trackerText.setBorderPen( QPen( c, 2 ) );
c.setAlpha( 200 );
trackerText.setBackgroundBrush( c );
QString info;
const QwtPlotItemList curves =
plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
for ( int i = 0; i < 1; i++ )
{
const QString curveInfo = curveInfoAt(
static_cast<const QwtPlotCurve *>( curves[i] ), pos );
if ( !curveInfo.isEmpty() )
{
if ( !info.isEmpty() )
info += "<br>";
info += curveInfo;
}
}
trackerText.setText( info );
return trackerText;
}
QString CurveTracker::curveInfoAt(
const QwtPlotCurve *curve, const QPointF &pos ) const
{
const QLineF line = curveLineAt( curve, pos.x() );
if ( line.isNull() )
return QString::null;
int x = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).x();
double y = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
// Info "window" args: color, x and y.
QString info( "<font color=""%1"">x : %2<br>y : %3</font>" );
return info.arg( "white" ).arg( x ).arg( QString::number(y, 'f', 3) ); // curve->pen().color().name()
}
QLineF CurveTracker::curveLineAt(
const QwtPlotCurve *curve, double x ) const
{
QLineF line;
if ( curve->dataSize() >= 2 )
{
const QRectF br = curve->boundingRect();
if ( br.isValid() && x >= br.left() && x <= br.right() )
{
int index = qwtUpperSampleIndex<QPointF>(
*curve->data(), x, compareX() );
if ( index == -1 &&
x == curve->sample( curve->dataSize() - 1 ).x() )
{
// the last sample is excluded from qwtUpperSampleIndex
index = curve->dataSize() - 1;
}
if ( index > 0 )
{
line.setP1( curve->sample( index - 1 ) );
line.setP2( curve->sample( index ) );
}
}
}
return line;
}
To copy to clipboard, switch view to plain text mode
In my plot class, I am using it like this:
// dataPlot_ = QwtPlot
CurveTracker* tracker = new CurveTracker( dataPlot_->canvas() );
tracker->setStateMachine( new QwtPickerTrackerMachine() );
tracker
->setRubberBandPen
( QPen(Qt
::black,
1) );
// dataPlot_ = QwtPlot
CurveTracker* tracker = new CurveTracker( dataPlot_->canvas() );
tracker->setStateMachine( new QwtPickerTrackerMachine() );
tracker->setRubberBandPen( QPen(Qt::black, 1) );
To copy to clipboard, switch view to plain text mode
Ok, it is working, but now I need to clean/reset my entire QwtPlot object.
I'm using the following command to do so:
dataPlot_
->detachItems
(QwtPlotItem::Rtti_PlotItem,
true);
dataPlot_->detachItems(QwtPlotItem::Rtti_PlotItem, true);
To copy to clipboard, switch view to plain text mode
but when I move the mouse over my QwtPlot my application break. Investigating the problem I discovered that it is crashing because of the CurveTracker.
How can I remove/delete the CurveTracker (QwtPlotPicker) from my dataPlot_ (QwtPlot) canvas?
Thanks
Added after 1 22 minutes:
I solved the problem by disabling my QwtPlotPicker:
tracker->setEnabled(false);
tracker->setEnabled(false);
To copy to clipboard, switch view to plain text mode
I don't know if it is the best/right solution, but it worked.
Anyway, if anyone knows a better way to do so please let me know.
Bookmarks