PDA

View Full Version : Click on a marker



d1mbarus
7th January 2016, 16:24
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.


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!";
}
}
}
}

Is there a better solution?

Uwe
8th January 2016, 11:20
Is there a better solution?
Not out of the box - if you have to deal with many, many markers you might need to introduce some spatial ordered index ( f.e a quadtree ).

Uwe

PS: Using x * x is faster than pow(x, 2 ) and you can avoid the sqrt by comparing x * x + y * y against 25.

d1mbarus
9th January 2016, 20:33
Not out of the box - if you have to deal with many, many markers you might need to introduce some spatial ordered index ( f.e a quadtree ).

The number of markers is less than 10. Can you tell us more about the quadtree? I have not found information about quadtree.

Uwe
10th January 2016, 09:20
The number of markers is less than 10.
Then iterating over the complete list will never be a problem.


Can you tell us more about the quadtree? I have not found information about quadtree.
https://en.wikipedia.org/wiki/Quadtree

Uwe