Hi,
I encounter a problem when using QwtPlotPannerand QwtPlotDirectPainter. It is described as following.
I make a plot widget by QwtPlot, and implement panner, picker and magnifier by inheriting QwtPlotPanner, QwtPlotPickand QwtPlotMagnifier.
The symbol selection is implemented by connect signal http://doc.qt.io/qt-4.8/QwtPlotPick::selected(QPointF) with a select(QPointF) function. I also write a highlight function for selected symbol, using QwtPlotDirectPainter, similar to the example of event_filter with qwt package. The code show as follow
The plot:
{
Q_OBJECT
public:
QwtFoilPlot
(QWidget *parent
= NULL);
....
}
QwtFoilPlot
::QwtFoilPlot( QWidget *parent
/*= NULL*/ ): curve_np(200)
{
......
// panner
p_panner = new CanvasPanner(this);
// picker
p_picker = new CanvasPicker(canvas());
p_picker
->setRubberBand
(QwtPicker::NoRubberBand);
......
}
class QwtFoilPlot: public QwtPlot
{
Q_OBJECT
public:
QwtFoilPlot(QWidget *parent = NULL);
....
}
QwtFoilPlot::QwtFoilPlot( QWidget *parent /*= NULL*/ ):
curve_np(200)
{
......
// panner
p_panner = new CanvasPanner(this);
// picker
p_picker = new CanvasPicker(canvas());
p_picker->setStateMachine(new QwtPickerClickPointMachine);
p_picker->setRubberBand(QwtPicker::NoRubberBand);
......
}
To copy to clipboard, switch view to plain text mode
The plot picker:
{
QOBJECT
public:
private:
.......
void showCursor(bool enable);
.......
int d_selectedPoint;
};
class CanvasPicker: public QObject
{
QOBJECT
public:
CanvasPicker(QwtPlotCanvas* p);
private:
void select(const QPoint &);
.......
void showCursor(bool enable);
.......
QwtPlotCurve *d_selectedCurve;
int d_selectedPoint;
};
To copy to clipboard, switch view to plain text mode
The method definition for this picker
void CanvasPicker::select( const QPointF& p )
{
double dist = 10e10;
int index = -1;
const QwtPlotItemList& itmList = plot()->itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
{
double d;
int idx = c->closestPoint(pos, &d);
if ( d < dist )
{
curve = c;
index = idx;
dist = d;
}
}
}
showCursor(false);
p_selectedCurve = NULL;
p_selectedPoint = -1;
if ( curve && dist < 10 ) // 10 pixels tolerance
{
p_selectedCurve = curve;
p_selectedPoint = index;
showCursor(true);
}
}
void CanvasPicker::showCursor(bool showIt)
{
if ( !p_selectedCurve )
return;
QwtSymbol *symbol
= const_cast<QwtSymbol
*>
( p_selectedCurve
->symbol
() );
const QBrush brush
= symbol
->brush
();
if ( showIt )
symbol->setBrush(symbol->brush().color().dark(180));
QwtPlotDirectPainter directPainter;
directPainter.drawSeries(p_selectedCurve, p_selectedPoint, p_selectedPoint);
if ( showIt )
symbol->setBrush(brush); // reset brush
}
void CanvasPicker::select( const QPointF& p )
{
QPoint pos = transform(p);
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 = (QwtPlotCurve*)(*it);
double d;
int idx = c->closestPoint(pos, &d);
if ( d < dist )
{
curve = c;
index = idx;
dist = d;
}
}
}
showCursor(false);
p_selectedCurve = NULL;
p_selectedPoint = -1;
if ( curve && dist < 10 ) // 10 pixels tolerance
{
p_selectedCurve = curve;
p_selectedPoint = index;
showCursor(true);
}
}
void CanvasPicker::showCursor(bool showIt)
{
if ( !p_selectedCurve )
return;
QwtSymbol *symbol = const_cast<QwtSymbol *>( p_selectedCurve->symbol() );
const QBrush brush = symbol->brush();
if ( showIt )
symbol->setBrush(symbol->brush().color().dark(180));
QwtPlotDirectPainter directPainter;
directPainter.drawSeries(p_selectedCurve, p_selectedPoint, p_selectedPoint);
if ( showIt )
symbol->setBrush(brush); // reset brush
}
To copy to clipboard, switch view to plain text mode
The panner
{
Q_OBJECT
public:
CanvasPanner(QwtFoilPlot *plot);
private:
};
CanvasPanner::CanvasPanner( QwtFoilPlot *plot ):
{
setMouseButton(Qt::MidButton);
}
class CanvasPanner: public QwtPlotPanner
{
Q_OBJECT
public:
CanvasPanner(QwtFoilPlot *plot);
private:
};
CanvasPanner::CanvasPanner( QwtFoilPlot *plot ):
QwtPlotPanner(plot->canvas())
{
setMouseButton(Qt::MidButton);
}
To copy to clipboard, switch view to plain text mode
The problem appear when I pan the canvas after a selection. When I pick a symbol on a curve by click, the symbol will be highlighted normally(1.png). Then I start to pan the canvas without releasing the mid button, the highlighted symbol will offset a little to upper left(2.png and 3.png). Then I release the button, the offset disappeared.
Did anyone see this problem before? Is there any simple way to solve this problem?
Thank you
1.png2.png3.png
Bookmarks