当前位置: 首页 > news >正文

QCustomPlot利用QCPItemStraightLine实现鼠标跟踪线

实现原理:在QCustomPlot添加两个QCPItemStraightLine对象分别用于鼠标X\Y轴跟踪线绘制,重写QCustomPlot的鼠标移动事件并捕获鼠标实时坐标传递至QCPItemStraightLine更新位置,QCustomPlot重绘进行效果呈现。具体实现需要重写QCustomPlot方法及封装QCPItemStraightLine的功能类。

1、封装QCPItemStraightLine的功能类

  1 #ifndef MOUSETRACELINE_H
  2 #define MOUSETRACELINE_H
  3 #include "qcustomplot.h"
  4 #include <QObject>
  5 
  6 enum LineType
  7 {
  8     VerticalLine,   //垂直线
  9     HorizonLine,    //水平线
 10     Both            //同时显示水平和垂直线
 11 };
 12 
 13 class MouseTraceLine : public QObject
 14 {
 15     Q_OBJECT
 16 public:
 17     explicit MouseTraceLine(QCustomPlot *_plot, LineType _type = VerticalLine,QObject *parent = nullptr);
 18     ~MouseTraceLine();
 19 
 20     void init_line();
 21     void updatePosition(double xValue, double yValue);
 22 protected:
 23     bool m_visible;//是否可见
 24     LineType m_type;//类型
 25     QCustomPlot *m_plot;//图表
 26     QCPItemStraightLine *m_lineV; //垂直线
 27     QCPItemStraightLine *m_lineH; //水平线
 28 
 29 signals:
 30 
 31 public slots:
 32 };
 33 
 34 #endif // MOUSETRACELINE_H
 35 
 36 #include "mousetraceline.h"
 37 
 38 MouseTraceLine::MouseTraceLine(QCustomPlot *_plot, LineType _type,QObject *parent) : QObject(parent),
 39     m_type(_type),
 40     m_plot(_plot)
 41 {
 42     m_lineH = Q_NULLPTR;
 43     m_lineV = Q_NULLPTR;
 44     init_line();
 45 }
 46 
 47 MouseTraceLine::~MouseTraceLine()
 48 {
 49     if(m_plot)
 50     {
 51         if(m_lineH)
 52             m_plot->removeItem(m_lineH);
 53         if(m_lineV)
 54             m_plot->removeItem(m_lineV);
 55     }
 56 }
 57 
 58 void MouseTraceLine::init_line()
 59 {
 60     if(m_plot)
 61     {
 62         QPen linesPen(Qt::red, 1, Qt::DashLine);
 63         if(VerticalLine == m_type || Both == m_type)
 64         {
 65             m_lineV = new QCPItemStraightLine(m_plot);//垂直线
 66             m_lineV->setPen(linesPen);
 67             m_lineV->point1->setCoords(0, 0);
 68             m_lineV->point2->setCoords(0, 0);
 69         }
 70 
 71         if(HorizonLine == m_type || Both == m_type)
 72         {
 73             m_lineH = new QCPItemStraightLine(m_plot);//水平线
 74             m_lineH->setPen(linesPen);
 75             m_lineH->point1->setCoords(0, 0);
 76             m_lineH->point2->setCoords(0, 0);
 77         }
 78     }
 79 }
 80 
 81 void MouseTraceLine::updatePosition(double xValue, double yValue)
 82 {
 83     if(VerticalLine == m_type || Both == m_type)
 84     {
 85         if(m_lineV)
 86         {
 87             m_lineV->point1->setCoords(xValue, m_plot->yAxis->range().lower);
 88             m_lineV->point2->setCoords(xValue, m_plot->yAxis->range().upper);
 89         }
 90     }
 91 
 92     if(HorizonLine == m_type || Both == m_type)
 93     {
 94         if(m_lineH)
 95         {
 96             m_lineH->point1->setCoords(m_plot->xAxis->range().lower, yValue);
 97             m_lineH->point2->setCoords(m_plot->xAxis->range().upper, yValue);
 98         }
 99     }
100 }

2、QCustomPlot重写

 1 #ifndef QCUSTOMPLOTEX_H
 2 #define QCUSTOMPLOTEX_H
 3 
 4 #include "qcustomplot.h"
 5 #include "mousetraceline.h"
 6 class QCustomPlotEx : public QCustomPlot
 7 {
 8     Q_OBJECT
 9 public:
10     QCustomPlotEx();
11 
12 protected:
13     virtual void mouseMoveEvent(QMouseEvent *event);
14 
15 private:
16     MouseTraceLine  *m_lineTracer;//直线
17 };
18 
19 #endif // QCUSTOMPLOTEX_H
20 
21 
22 #include "qcustomplotex.h"
23 
24 QCustomPlotEx::QCustomPlotEx()
25 {
26     m_lineTracer = new MouseTraceLine(this,LineType::Both);
27 }
28 
29 void QCustomPlotEx::mouseMoveEvent(QMouseEvent *event)
30 {
31     QCustomPlot::mouseMoveEvent(event);
32     //当前鼠标位置(像素坐标)
33     int x_pos = event->pos().x();
34     int y_pos = event->pos().y();
35 
36     //像素坐标转成实际的x,y轴的坐标
37     double x_val = this->xAxis->pixelToCoord(x_pos);
38     double y_val = this->yAxis->pixelToCoord(y_pos);
39 
40     m_lineTracer->updatePosition(x_val, y_val);
41     this->replot();
42 }

鼠标跟踪

http://www.jsqmd.com/news/341596/

相关文章:

  • 在Cursor中使用Skills功能
  • 成为高级电源工程师的必备工具-MathCAD
  • 2026年性价比高的信用贷款公司有哪些?说说信用贷款公司选择要点 - mypinpai
  • 沃尔玛购物卡快速回收:教你变现的最佳攻略 - 团团收购物卡回收
  • invalid byte sequence for encoding
  • 2026年 NFC卡定制厂家推荐排行榜:NFC电子标签/NFC芯片贴/NFC门禁卡/NFC防伪标签,创新智能与耐用品质的源头之选 - 品牌企业推荐师(官方)
  • 2026年环境工程服务商选购指南,盈尚环境工程有限公司服务商技术水平怎样 - 工业设备
  • 必收藏|人工智能体入门详解(小白/程序员必看):不是聊天机器人,是未来超级助手
  • 兴义市英语雅思培训机构推荐?2026权威测评出国雅思辅导机构口碑榜单 - 老周说教育
  • 福泉市英语雅思培训机构推荐|2026权威测评出国雅思辅导机构口碑榜单 - 老周说教育
  • 红队避坑封神!4 个致命操作差点行业拉黑,合法攻击边界 + 信息收集实战全解析
  • 机械手上下料的PLC 控制设计(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 必读:山东菏泽测试团队的全球化远程协作宪章
  • 成都恒利泰:国产LTCC滤波器的逆袭之路
  • 聊聊卡西欧手表批发,靠谱的品牌企业有哪些值得选 - 工业品网
  • 区块链存证技术原理与软件测试的契合性
  • 2026年国有资产管理系统私有化部署 不动产管理公司推荐 - 品牌2025
  • 2026年贵州地区环形轻质瓦斯抽放管生产商排名,哪家性价比高? - 工业推荐榜
  • 2026国内最新木纹板材生产厂家TOP5推荐:环保家装/全屋定制柜体板材品牌权威榜单发布,多场景适配助力品质家居打造 - 品牌推荐2026
  • 聊聊2026年资质齐全的氯化镁品牌产品,哪家性价比高 - mypinpai
  • 计算机毕业设计之jsp惠友电子产品网上商城的设计与实现
  • 开发 / 运维转网安简历攻略!2026 重构指南 + 模板,不会写简历也能拿面试
  • 2026年AI配音软件服务排名,哪家性价比高值得推荐 - 工业设备
  • 2026排烟风机厂家品牌推荐:排烟风机公司哪家好? - 博客万
  • 上门家政服务小程序盈利模式分析(附技术落地要点)
  • 乙方驻场转网安捷径!两大核心经验直接复用,转型不绕弯,轻松超车
  • 2026年杭州地区全国口碑好的讲抖音落地老师推荐 - myqiye
  • 2026年筒子饵块机选购指南,云南靠谱的生产商排名揭晓 - myqiye
  • 2026年四川环形轻质瓦斯抽放管品牌推荐,密封性好的靠谱之选 - 工业推荐榜
  • 红队避坑封神攻略!4 个操作差点被行业拉黑,合法攻击边界 + 信息收集实战全指南