Design:
Here I have design a graph where I can plot some signals like oscilloscope. Now I need two vertical and two horizontal lines on the graph which can move vertically and horizontally and also show cross points(symbol) when any signals get cross this lines.

Qt Code:
  1. PlotterWidget::PlotterWidget(QWidget *parent)
  2. : QGraphicsView( parent ),
  3. layout(new QVBoxLayout(this)), plot(new QwtPlot()), legend(new QwtLegend()),
  4. m_zoomP(new PlotZoomer(QwtPlot::xBottom, QwtPlot::yLeft, plot->canvas()))
  5. {
  6. layout->setAlignment(plot->canvas(), Qt::AlignTop);
  7. layout->setSizeConstraint(QLayout::SetDefaultConstraint);
  8. plot->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
  9.  
  10.  
  11. layout->addWidget(plot);
  12.  
  13. plot->plotLayout()->setAlignCanvasToScales(true);//update on crossing the graph
  14.  
  15. m_zoomM[0] = new QwtPlotMagnifier(plot->canvas()); // ( void ) new QwtPlotMagnifier( canvas );
  16. m_zoomM[0]->setWheelModifiers(Qt::NoModifier); //Scroll --> Both Axis
  17.  
  18. m_zoomM[1] = new QwtPlotMagnifier(plot->canvas());
  19. m_zoomM[1]->setWheelModifiers(Qt::ControlModifier); //Ctrl + Scroll --> on X-axis
  20. m_zoomM[1]->setAxisEnabled(Qt::XAxis,false);
  21.  
  22.  
  23. QWidget *horizontalLineWidget = new QWidget(plot->canvas());
  24. horizontalLineWidget->setFixedHeight(2);
  25. horizontalLineWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  26. horizontalLineWidget->setStyleSheet(QString("background-color: #c0c0c0;"));
  27.  
  28. QwtPlotGrid *grid = new QwtPlotGrid();
  29. grid->setPen(QPen(Qt::gray, 0.0, Qt::SolidLine));
  30. grid->enableX(true);
  31. grid->enableXMin(true);
  32. grid->enableY(true);
  33. grid->enableYMin(true);
  34. grid->attach(plot);
  35.  
  36. legend->setDefaultItemMode(QwtLegendData::Checkable);
  37. QwtSymbol *sym=new QwtSymbol(QwtSymbol::Diamond,QBrush(Qt::red),QPen(Qt::red),QSize(5,5));
  38. d_marker1 = new QwtPlotMarker();
  39. d_marker1->setSymbol(sym);
  40. d_marker1->setLegendIconSize(QSize(20,30));
  41. d_marker1->setLabel(QwtText("VLine1"));
  42. d_marker1->setRenderHint(QwtPlotItem::RenderHint::RenderAntialiased);
  43. d_marker1->setTitle(QwtText("VLine1"));
  44. d_marker1->setLabelOrientation(Qt::Vertical);
  45. d_marker1->setValue(333, 0);
  46. d_marker1->setLineStyle(QwtPlotMarker::VLine);
  47. d_marker1->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
  48. d_marker1->setLinePen(QPen(Qt::blue, 1, Qt::DashDotLine));
  49. d_marker1->setItemAttribute( QwtPlotItem::Legend, true);
  50.  
  51. d_marker1->attach(plot);
  52.  
  53. d_marker2 = new QwtPlotMarker();
  54. d_marker2->setSymbol(sym);
  55. d_marker2->setValue(0, 333);
  56. d_marker2->setLabel(QwtText("HLine1"));
  57. d_marker2->setLineStyle(QwtPlotMarker::HLine);
  58. d_marker2->setTitle(QwtText("HLine1"));
  59. d_marker2->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
  60. d_marker2->setLinePen(QPen(Qt::blue, 1, Qt::DashDotLine));
  61. d_marker2->attach(plot);
  62.  
  63. d_picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
  64. QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
  65. plot->canvas());
  66. d_picker1 = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
  67. QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
  68. plot->canvas());
  69.  
  70. d_picker->setStateMachine(new QwtPickerDragPointMachine);
  71. d_picker1->setStateMachine(new QwtPickerClickPointMachine);
  72.  
  73. d_picker->setRubberBandPen(QColor(Qt::green));
  74. d_picker->setTrackerPen(QColor(Qt::blue));
  75. d_picker->setEnabled(true);
  76.  
  77. connect(d_picker1, SIGNAL(selected(const QPointF &)),
  78. SLOT(selected(const QPointF &)));
  79.  
  80.  
  81. plot->setFocusProxy(plot->canvas());
  82. plot->setFocusPolicy(Qt::StrongFocus);
  83. plot->setFocus();
  84. plot->installEventFilter(this);
  85.  
  86.  
  87. plot->setAutoReplot(true);
  88. setScaleType("Auto");
  89.  
  90. plot->insertLegend(legend, QwtPlot::RightLegend );
  91. plot->replot();
  92.  
  93. connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ), SLOT( &Input::legendChecked( const QVariant &, bool ) ) );
  94. }
To copy to clipboard, switch view to plain text mode 

And to pick the Line I have write below code

Qt Code:
  1. void PlotterWidget::selected(const QPointF &pos)
  2. {
  3. qDebug()<<"selected";
  4. const QwtPlotItemList& items = plot->itemList();
  5.  
  6. QMetaObject MetaObject = this->staticMetaObject;
  7. QMetaEnum MetaEnum = MetaObject.enumerator(MetaObject.indexOfEnumerator("MarkLine"));
  8.  
  9. for ( QwtPlotItemIterator i = items.begin(); i != items.end(); ++i )
  10. {
  11. if ( (*i)->rtti() == QwtPlotItem::Rtti_PlotMarker )
  12. {
  13. m_mark = static_cast<QwtPlotMarker*>(*i);
  14. if(!m_mark->isVisible())
  15. {
  16. continue;
  17. }
  18. if (sqrt( pow( (m_mark->xValue() - pos.x()), 2 )) <= 5 || sqrt( pow( (m_mark->yValue() - pos.y()), 2 )) <=5)
  19. {
  20. setLineMarker(static_cast<MarkLine>(MetaEnum.keysToValue(m_mark->label().text().toLatin1())));
  21. switch(getLineMarker())
  22. {
  23. case VLine1:
  24. d_marker1->setLinePen(QPen(Qt::blue, 3, Qt::SolidLine));
  25. break;
  26. case HLine1:
  27. d_marker2->setLinePen(QPen(Qt::blue, 3, Qt::SolidLine));
  28. break;
  29. };
  30. connect(d_picker, SIGNAL(moved(const QPoint &)),
  31. SLOT(moved(const QPoint &)));
  32. }
  33. }
  34. }
  35. }
To copy to clipboard, switch view to plain text mode 


Problem:
I can able to pick line on grid when its of size x(0-1000) and y(0-1000), but the problem is when I'm trying to zoom in/out too much then I cant able to pass this below IF statement properly, because the distance is too less to pick Line.
Qt Code:
  1. if (sqrt( pow( (m_mark->xValue() - pos.x()), 2 )) <= 5 || sqrt( pow( (m_mark->yValue() - pos.y()), 2 )) <=5)
To copy to clipboard, switch view to plain text mode 

Need solution how to pick this line properly without affecting zooming the graph ?
It is possible during Zooming, make QwtPlotMarker not get move along with the grid or not zoomed?
And also while zooming, QwtPlotMarker Line get hidden somewhere and difficult to find, so how to deal with this ?

I really get stuck in this, please show me the way.

Thanks a lot for reading this post.