Results 1 to 5 of 5

Thread: Get the plot curve to which the mouse is pointing to

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2009
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Get the plot curve to which the mouse is pointing to

    I found a solution by implementing a custom PlotPicker and evaluating following method within the trackerText() method.

    Qt Code:
    1. bool PlotPicker::curveSelected(const QPoint pMousePos, PlotCurve* &pPlotCurve, double &pYValue) const
    2. {
    3. double lDist = 10e10;
    4. int lIndex = -1;
    5.  
    6. // get nearest curve point by iterating over plot items, filtering for curves and calculating minimum distance
    7. const QwtPlotItemList& lPlotItemList = plot()->itemList();
    8. for ( QwtPlotItemIterator lPlotItemIterator = lPlotItemList.begin();
    9. lPlotItemIterator != lPlotItemList.end(); ++lPlotItemIterator )
    10. {
    11. if ( (*lPlotItemIterator)->rtti() == QwtPlotItem::Rtti_PlotCurve )
    12. {
    13. PlotCurve *lTmpCurve = (PlotCurve*)(*lPlotItemIterator);
    14.  
    15. double lTmpDist;
    16. int lTmpIndex = lTmpCurve->closestPoint(pMousePos, &lTmpDist);
    17. if ( lTmpDist < lDist && lTmpIndex > -1)
    18. {
    19. pPlotCurve = lTmpCurve;
    20. lDist = lTmpDist;
    21. lIndex = lTmpIndex;
    22. }
    23. }
    24. }
    25.  
    26. // check if mouse position is in tolerance
    27. if ( pPlotCurve && lDist < _pix_tolerance_ )
    28. {
    29. pYValue = pPlotCurve->y(lIndex);
    30. return true;
    31. }
    32. pPlotCurve = 0;
    33. return false;
    34. }
    35.  
    36. QwtText PlotPicker::trackerText(const QPoint &pMousePos) const
    37. {
    38. QwtText lText;
    39. double lSelectedYValue = 0.0;
    40. PlotCurve* lSelectedCurve = 0;
    41. if(curveSelected(pMousePos, lSelectedCurve, lSelectedYValue)) {
    42. // ... the code creating the label at the cursor position
    43. }
    44. return lText;
    45. }
    To copy to clipboard, switch view to plain text mode 

    In advance I implemented there also a vertical marker following the cursor position. By adding a horizontal marker we would get crosshair functionality...

    Qt Code:
    1. PlotPicker::PlotPicker(QwtPlot *pPlot) :
    2. QwtPlotPicker(pPlot->canvas())
    3. {
    4. // create vertical mouse marker
    5. mMouseMarker = new QwtPlotMarker();
    6. mMouseMarker->attach(pPlot);
    7. mMouseMarker->setVisible(false);
    8. mMouseMarker->setLineStyle(QwtPlotMarker::VLine);
    9. mMouseMarker->setLinePen(QPen(Qt::DotLine));
    10. }
    11.  
    12. void PlotPicker::widgetMouseMoveEvent(QMouseEvent *pMouseEvent)
    13. {
    14. QwtPlotPicker::widgetMouseMoveEvent(pMouseEvent);
    15. qreal lXPosition = invTransform(pMouseEvent->pos()).x();
    16. mMouseMarker->setXValue(lXPosition);
    17. mMouseMarker->setVisible(true);
    18. plot()->replot();
    19. }
    20.  
    21. void PlotPicker::widgetLeaveEvent(QEvent *pEvent)
    22. {
    23. QwtPlotPicker::widgetLeaveEvent(pEvent);
    24. mMouseMarker->setVisible(false);
    25. plot()->replot();
    26. }
    To copy to clipboard, switch view to plain text mode 

    ... and it works!

    Regards
    Stefan

  2. The following user says thank you to StefanK for this useful post:

    cnbp173 (31st January 2009)

Similar Threads

  1. Moving plot marker with the mouse
    By viridis in forum Qwt
    Replies: 2
    Last Post: 23rd September 2008, 20:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.