Hello! The plot (QwtPlot) contains many markers (QwtPlotMarker). How to determine which marker was clicked?
To solve this problem, I changed the standard example of "event filter". After each click of the mouse, I check all the markers.
If the marker is located close to the click, this marker has been pressed.
{
const QwtPlotItemList& items = itemList();
for ( QwtPlotItemIterator i = items.begin(); i != items.end(); ++i )
{
{
// The distance from the marker to the place a clicked
float distance
= sqrt( pow( (transform
(QwtPlot::xBottom, m
->value
().
x()) - e
->pos
().
x()),
2 ) + pow( (transform
(QwtPlot::yLeft, m
->value
().
y()) - e
->pos
().
y()),
2 ) );
if (distance <= 5)
{
qDebug() << "YEAP!";
}
}
}
}
void Plot::mousePressEvent(QMouseEvent *e)
{
QwtPlot::mousePressEvent(e);
const QwtPlotItemList& items = itemList();
for ( QwtPlotItemIterator i = items.begin(); i != items.end(); ++i )
{
if ( (*i)->rtti() == QwtPlotItem::Rtti_PlotMarker )
{
QwtPlotMarker *m = static_cast<QwtPlotMarker*>(*i);
// The distance from the marker to the place a clicked
float distance = sqrt( pow( (transform(QwtPlot::xBottom, m->value().x()) - e->pos().x()), 2 ) + pow( (transform(QwtPlot::yLeft, m->value().y()) - e->pos().y()), 2 ) );
if (distance <= 5)
{
qDebug() << "YEAP!";
}
}
}
}
To copy to clipboard, switch view to plain text mode
Is there a better solution?
Bookmarks