QCustomPlot坐标轴游标(显示当前x、y坐标值)
注:在坐标轴添加游标时,单独x轴或单独y轴应用游标无异常,当双轴均显示游标时会出现卡顿情况,需要在.pro中添加如下OpenGL的应用即可解决。
1 LIBS+=-lopengl32 -lglu32 2 DEFINES += QCUSTOMPLOT_USE_OPENGL
1、与鼠标跟随一样,建立游标类如下
1 #ifndef AXISTRACER_H 2 #define AXISTRACER_H 3 4 #include <QObject> 5 #include "qcustomplot.h" 6 enum TracerType 7 { 8 XAxisTracer=0,//依附在x轴上显示x值 9 YAxisTracer=1,//依附在y轴上显示y值 10 DataTracer=2,//在图中显示x,y值 11 MouseMarker=3//固定游标 12 }; 13 14 class AxisTracer : public QObject 15 { 16 Q_OBJECT 17 public: 18 explicit AxisTracer(QCustomPlot *_plot, TracerType _type, QObject *parent = nullptr); 19 20 void updatePosition(double xValue, double yValue); 21 22 protected: 23 TracerType m_type;//类型 24 QCustomPlot *m_plot;//图表 25 QCPItemTracer *m_tracer;//跟踪的点 26 QCPItemText *m_label;//显示的数值 27 QCPItemLine *m_arrow;//箭头 28 29 signals: 30 31 public slots: 32 }; 33 34 #endif // AXISTRACER_H 35 36 37 #include "axistracer.h" 38 39 AxisTracer::AxisTracer(QCustomPlot *_plot, TracerType _type, QObject *parent) : QObject(parent), 40 m_type(_type), 41 m_plot(_plot) 42 { 43 if(m_plot) 44 { 45 QColor clrDefault(Qt::red); 46 QBrush brushDefault(Qt::NoBrush); 47 QPen penDefault(clrDefault); 48 penDefault.setWidthF(0.5); 49 50 m_tracer = new QCPItemTracer(m_plot); 51 m_tracer->setStyle(QCPItemTracer::tsCircle); 52 m_tracer->setPen(penDefault); 53 m_tracer->setBrush(brushDefault); 54 55 m_label = new QCPItemText(m_plot); 56 m_label->setLayer("overlay"); 57 m_label->setClipToAxisRect(false); 58 m_label->setPadding(QMargins(5, 5, 5, 5)); 59 m_label->setBrush(brushDefault); 60 m_label->setPen(penDefault); 61 m_label->position->setParentAnchor(m_tracer->position); 62 m_label->setFont(QFont("Arial", 8)); 63 m_label->setColor(clrDefault); 64 m_label->setText(""); 65 66 m_arrow = new QCPItemLine(m_plot); 67 m_arrow->setPen(penDefault); 68 m_arrow->setLayer("overlay"); 69 m_arrow->setClipToAxisRect(false); 70 m_arrow->setHead(QCPLineEnding::esSpikeArrow);//设置头部为箭头形状 71 72 switch(m_type) 73 { 74 case XAxisTracer: 75 { 76 m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords); 77 m_tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio); 78 m_tracer->setSize(7); 79 m_label->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter); 80 81 m_arrow->end->setParentAnchor(m_tracer->position); 82 m_arrow->start->setParentAnchor(m_arrow->end); 83 m_arrow->start->setCoords(0, 20);//偏移量 84 } 85 break; 86 case YAxisTracer: 87 { 88 m_tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio); 89 m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords); 90 m_tracer->setSize(7); 91 m_label->setPositionAlignment(Qt::AlignRight | Qt::AlignHCenter); 92 93 m_arrow->end->setParentAnchor(m_tracer->position); 94 m_arrow->start->setParentAnchor(m_label->position); 95 m_arrow->start->setCoords(-20, 0);//偏移量 96 } 97 break; 98 case DataTracer: 99 break; 100 case MouseMarker: 101 break; 102 } 103 } 104 } 105 106 void AxisTracer::updatePosition(double xValue, double yValue) 107 { 108 if (yValue > m_plot->yAxis->range().upper) 109 yValue = m_plot->yAxis->range().upper; 110 111 switch (m_type) 112 { 113 case XAxisTracer: 114 { 115 m_tracer->position->setCoords(xValue, 1); 116 m_label->position->setCoords(0, 15); 117 m_arrow->start->setCoords(0, 15); 118 m_arrow->end->setCoords(0, 0); 119 m_label->setText(QString::number(xValue)); 120 break; 121 } 122 case YAxisTracer: 123 { 124 m_tracer->position->setCoords(0, yValue); 125 m_label->position->setCoords(-20, 0); 126 m_arrow->start->setCoords(-20, 0); 127 m_arrow->end->setCoords(0, 0); 128 m_label->setText(QString::number(yValue)); 129 break; 130 } 131 case DataTracer: 132 { 133 break; 134 } 135 case MouseMarker: 136 break; 137 } 138 }
2、QCustomPlot重写进行游标功能呈现
1 #ifndef QCUSTOMPLOTEX_H 2 #define QCUSTOMPLOTEX_H 3 4 #include "qcustomplot.h" 5 #include "mousetraceline.h" 6 #include "axistracer.h" 7 class QCustomPlotEx : public QCustomPlot 8 { 9 Q_OBJECT 10 public: 11 QCustomPlotEx(); 12 13 protected: 14 virtual void mouseMoveEvent(QMouseEvent *event); 15 16 private: 17 MouseTraceLine *m_lineTracer;//鼠标跟随线 18 19 AxisTracer *m_xTracer;//x轴游标 20 AxisTracer *m_yTracer;//y轴游标 21 }; 22 23 #endif // QCUSTOMPLOTEX_H 24 25 26 #include "qcustomplotex.h" 27 28 QCustomPlotEx::QCustomPlotEx() 29 { 30 m_lineTracer = new MouseTraceLine(this,LineType::Both); 31 m_xTracer = new AxisTracer(this, TracerType::XAxisTracer);//x轴 32 m_yTracer = new AxisTracer(this, TracerType::YAxisTracer);//y轴 33 } 34 35 void QCustomPlotEx::mouseMoveEvent(QMouseEvent *event) 36 { 37 QCustomPlot::mouseMoveEvent(event); 38 //当前鼠标位置(像素坐标) 39 int x_pos = event->pos().x(); 40 int y_pos = event->pos().y(); 41 42 //像素坐标转成实际的x,y轴的坐标 43 double x_val = this->xAxis->pixelToCoord(x_pos); 44 double y_val = this->yAxis->pixelToCoord(y_pos); 45 m_lineTracer->updatePosition(x_val, y_val); 46 47 m_xTracer->updatePosition(x_val, y_val); 48 m_yTracer->updatePosition(x_val, y_val); 49 this->replot(); 50 }

记性太差,需要这么记下来

浙公网安备 33010602011771号