设为首页 加入收藏

TOP

基于QCustomPlot绘图,鼠标跟随动态显示曲线上的点的值(一)
2018-12-11 16:09:42 】 浏览:1269
Tags:基于 QCustomPlot 绘图 鼠标 跟随 动态 显示 曲线
QCustomPlot是一个开源的基于Qt的第三方绘图库,能够绘制漂亮的2D图形。
QCustomPlot的官方网址:https://www.qcustomplot.com/
从官网下载QCustomPlot的源文件,包括qcustomplot.h和qcustomplot.cpp。
 
1 自定义鼠标显示跟随类XxwTracer和XxwTraceLine:
XxwTracer用于在图表中显示鼠标所在位置的x,y值
XxwTraceLine用于在图中显示水平或垂直的虚线
头文件XxwTracer.h
#ifndef MYTRACER_H #define MYTRACER_H #include <QObject> #include "qcustomplot.h"

///
/// \brief The XxwTracer class:在图表中显示鼠标所在位置的x,y值的追踪显示器 /// class XxwTracer : public QObject { Q_OBJECT public: enum TracerType { XAxisTracer,//依附在x轴上显示x值
        YAxisTracer,//依附在y轴上显示y值
        DataTracer//在图中显示x,y值
}; explicit XxwTracer(QCustomPlot *_plot, TracerType _type, QObject *parent = Q_NULLPTR); ~XxwTracer(); void setPen(const QPen &pen); void setBrush(const QBrush &brush); void setText(const QString &text); void setLabelPen(const QPen &pen); void updatePosition(double xValue, double yValue); void setVisible(bool m_visible); protected: bool m_visible;//是否可见
    TracerType m_type;//类型
    QCustomPlot *m_plot;//图表
    QCPItemTracer *m_tracer;//跟踪的点
    QCPItemText *m_label;//显示的数值
    QCPItemLine *m_arrow;//箭头
}; ///
/// \brief The XxwCrossLine class:用于显示鼠标移动过程中的鼠标位置的直线 /// class XxwTraceLine : public QObject { public: enum LineType { VerticalLine,//垂直线
        HorizonLine, //水平线
        Both//同时显示水平和垂直线
 }; explicit XxwTraceLine(QCustomPlot *_plot, LineType _type = VerticalLine, QObject *parent = Q_NULLPTR); ~XxwTraceLine(); void initLine(); void updatePosition(double xValue, double yValue); void setVisible(bool vis) { if(m_lineV) m_lineV->setVisible(vis); if(m_lineH) m_lineH->setVisible(vis); } protected: bool m_visible;//是否可见
    LineType m_type;//类型
    QCustomPlot *m_plot;//图表
    QCPItemStraightLine *m_lineV; //垂直线
    QCPItemStraightLine *m_lineH; //水平线
}; #endif // MYTRACER_H

源文件MyTracer.cpp

#include "MyTracer.h" XxwTracer::XxwTracer(QCustomPlot *_plot, TracerType _type, QObject *parent) : QObject(parent), m_plot(_plot), m_type(_type) { m_visible = true; m_tracer = Q_NULLPTR;// 跟踪的点
    m_label = Q_NULLPTR;// 显示的数值
    m_arrow = Q_NULLPTR;// 箭头
    if (m_plot) { QColor clrDefault(Qt::red); QBrush brushDefault(Qt::NoBrush); QPen penDefault(clrDefault); // penDefault.setBrush(brushDefault);
        penDefault.setWidthF(0.5); m_tracer = new QCPItemTracer(m_plot); m_tracer->setStyle(QCPItemTracer::tsCircle); m_tracer->setPen(penDefault); m_tracer->setBrush(brushDefault); m_label = new QCPItemText(m_plot); m_label->setLayer("overlay"); m_label->setClipToAxisRect(false); m_label->setPadding(QMargins(5, 5, 5, 5)); m_label->setBrush(brushDefault); m_label->setPen(penDefault); m_label->position->setParentAnchor(m_tracer->position); // m_label->setFont(QFont("宋体", 8));
        m_label->setFont(QFont("Arial", 8)); m_label->setColor(clrDefault); m_label->setText(""); m_arrow = new QCPItemLine(m_plot); QPen arrowPen(clrDefault, 1); m_arrow->setPen(penDefault); m_arrow->setLayer("overlay"); m_arrow->setClipToAxisRect(false); m_arrow->setHead(QCPLineEnding::esSpikeArrow);//设置头部为箭头形状

        switch (m_type) { case XAxisTracer: { m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords); m_tracer->position->setTypeY(QCP
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[bzoj3524][Couriers] 下一篇[luogu3810][bzoj3262][陌上花开]

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目