采用Qt快速绘制多条曲线(折线),跟随鼠标动态显示线上点的值(基于Qt的开源绘图控件QCu。。。

采⽤Qt快速绘制多条曲线(折线),跟随⿏标动态显⽰线上点的值(基于Qt的开源绘图控件QCu。。。
QCustomPlot是⼀个开源的基于Qt的第三⽅绘图库,能够绘制漂亮的2D图形。
QCustomPlot的官⽅⽹址:
从官⽹下载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,//垂直线
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(QCPItemPosition::ptAxisRectRatio);
m_tracer->setSize(7);
m_label->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
m_arrow->end->setParentAnchor(m_tracer->position);
m_arrow->start->setParentAnchor(m_arrow->end);
m_arrow->start->setCoords(0, 20);//偏移量
break;
}
case YAxisTracer:
{
m_tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);
m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
m_tracer->setSize(7);
m_label->setPositionAlignment(Qt::AlignRight | Qt::AlignHCenter);
m_arrow->end->setParentAnchor(m_tracer->position);
m_arrow->start->setParentAnchor(m_label->position);
m_arrow->start->setCoords(-20, 0);//偏移量
break;
}
case DataTracer:
{
m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
m_tracer->setSize(5);
m_label->setPositionAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_arrow->end->setParentAnchor(m_tracer->position);
m_arrow->start->setParentAnchor(m_arrow->end);
m_arrow->start->setCoords(20, 0);
break;
}
default:
break;
}
setVisible(false);
}
}
XxwTracer::~XxwTracer()
{
if(m_plot)
{
if (m_tracer)
m_plot->removeItem(m_tracer);
if (m_label)
m_plot->removeItem(m_label);
if (m_arrow)
m_plot->removeItem(m_arrow);
}
}
}
void XxwTracer::setPen(const QPen &pen)
{
if(m_tracer)
m_tracer->setPen(pen);
if(m_arrow)
m_arrow->setPen(pen);
}
void XxwTracer::setBrush(const QBrush &brush)
{
if(m_tracer)
m_tracer->setBrush(brush);
}
void XxwTracer::setLabelPen(const QPen &pen)
{
if(m_label)
{
m_label->setPen(pen);
m_label->setBrush(Qt::NoBrush);
m_label->lor());
}
}
void XxwTracer::setText(const QString &text)
{
if(m_label)
m_label->setText(text);
}
void XxwTracer::setVisible(bool vis)
{
m_visible = vis;
if(m_tracer)
涡轮抽风机
m_tracer->setVisible(m_visible);
if(m_label)
m_label->setVisible(m_visible);
if(m_arrow)
m_arrow->setVisible(m_visible);
}
void XxwTracer::updatePosition(double xValue, double yValue) {
if (!m_visible)
{
setVisible(true);
m_visible = true;
}
if (yValue > m_plot->yAxis->range().upper)
yValue = m_plot->yAxis->range().upper;
switch (m_type)
{
case XAxisTracer:
{
m_tracer->position->setCoords(xValue, 1);
m_label->position->setCoords(0, 15);
m_arrow->start->setCoords(0, 15);
m_arrow->end->setCoords(0, 0);
setText(QString::number(xValue));
break;
}
case YAxisTracer:
case YAxisTracer:
{
m_tracer->position->setCoords(0, yValue);
m_label->position->setCoords(-20, 0);
//        m_arrow->start->setCoords(20, 0);
//        m_arrow->end->setCoords(0, 0);
setText(QString::number(yValue));
break;
}
case DataTracer:
{
m_tracer->position->setCoords(xValue, yValue);
m_label->position->setCoords(20, 0);
setText(QString("x:%1,y:%2").arg(xValue).arg(yValue));
break;
}
舵角指示器default:
break;
}
}
XxwTraceLine::XxwTraceLine(QCustomPlot *_plot, LineType _type, QObject *parent)    : QObject(parent),
m_type(_type),
m_plot(_plot)
{
m_lineV = Q_NULLPTR;
m_lineH = Q_NULLPTR;
initLine();发电机集电环
}
XxwTraceLine::~XxwTraceLine()
{
if(m_plot)
{
if (m_lineV)
m_plot->removeItem(m_lineV);
if (m_lineH)
m_plot->removeItem(m_lineH);
}
}
void XxwTraceLine::initLine()
{
if(m_plot)
{
QPen linesPen(Qt::red, 1, Qt::DashLine);
if(VerticalLine == m_type || Both == m_type)
{
m_lineV = new QCPItemStraightLine(m_plot);//垂直线
m_lineV->setLayer("overlay");
m_lineV->setPen(linesPen);
m_lineV->setClipToAxisRect(true);
m_lineV->point1->setCoords(0, 0);
m_lineV->point2->setCoords(0, 0);
}
if(HorizonLine == m_type || Both == m_type)
{
m_lineH = new QCPItemStraightLine(m_plot);//⽔平线
m_lineH->setLayer("overlay");
m_lineH->setPen(linesPen);
m_lineH->setClipToAxisRect(true);
锂电池注液机
m_lineH->point1->setCoords(0, 0);
m_lineH->point2->setCoords(0, 0);

本文发布于:2024-09-22 17:17:56,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/164385.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:绘制   跟随   折线   程序   设置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议