qcustomplot 游标_人过四十不学艺——QCustomPlot设置游标详细笔记

在QT下开发虚拟示波器程序时,需要用到曲线显示控件,开源的有QCustomPlot和QWT,其中QCustomPlot可以直接利用其源码,而不需要使用链接库的方式,因此,得到了大量的应用,为了简化,我使用了QCustomPlot 1.3.0的源代码并进行了修改实现完整的功能。

1、X轴和Y轴坐标迹线

常见示波器上,可以设置X轴和Y轴坐标迹线并且可以移动。为此,在借鉴网上代码(http://www.manongjc.com/article/22306.html基于QCustomPlot绘图,鼠标跟随动态显示曲线上的点的值)基础上,定义ScopeTraceLine类,与网上提供代码相比,增加了代表位置的坐标值变量和获取属性函数,并做出了相应的修改。

///

/// \brief The ScopeCrossLineclass:用于显示鼠标移动过程中的鼠标位置的直线

///

class ScopeTraceLine : publicQObject

{

public:

   explicit ScopeTraceLine(QCustomPlot *_plot, LineType _type =VerticalLine, QObject *parent = Q_NULLPTR);

   ~ScopeTraceLine();

   void initLine();

   void updatePosition(double xValue, double yValue);

   void setSelected(bool selected);

   void setVisible(bool vis);

   LineType GetType()const{return m_type;}

   double getPositionX()const{return m_xValue;}

   double getPositionY()const{return m_yValue;}

protected:

   bool m_visible;//是否可见

   LineType m_type;//类型

   QCustomPlot *m_plot;//图表

   QCPItemStraightLine *m_lineV; //垂直线

   QCPItemStraightLine *m_lineH; //水平线

   double m_xValue;

   double m_yValue;

};

具体实现函数如下:

ScopeTraceLine::ScopeTraceLine(QCustomPlot *_plot, LineType _type, QObject*parent)

    : QObject(parent),

      m_type(_type),

      m_plot(_plot)

{

    m_lineV = Q_NULLPTR;

    m_lineH = Q_NULLPTR;

    m_xValue= -999;

    m_yValue= -999;

    initLine();

}

ScopeTraceLine::~ScopeTraceLine()

{

    if(m_plot)

    {

        if (m_lineV)

           m_plot->removeItem(m_lineV);

        if (m_lineH)

           m_plot->removeItem(m_lineH);

    }

}

void ScopeTraceLine::initLine()

{

    if(m_plot)

    {

        QPenlinesPen(Qt::green, 1, Qt::DashLine);

        if(VerticalLine ==m_type || Both == m_type)

        {

            m_lineV = newQCPItemStraightLine(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 = newQCPItemStraightLine(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);

        }

    }

}

void ScopeTraceLine::setVisible(bool vis)

{

    if(m_lineV)

        m_lineV->setVisible(vis);

    if(m_lineH)

       m_lineH->setVisible(vis);

}

void ScopeTraceLine::setSelected(bool selected)

{

    QPen linesPen(Qt::green,1, Qt::DashLine);

    if(selected)

       linesPen.setColor(Qt::red);

    if(VerticalLine == m_type|| Both == m_type)

    {

        if(Q_NULLPTR !=m

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C知道: CSDN 上有一个名为 "QCustomPlot" 的开源库,它提供了丰富的图表绘制功能,包括游标功能。 QCustomPlot游标功能允许用户在图表上添加一个或多个垂直或水平的游标线,并根据鼠标位置实时更新游标的位置。这对于在图表上标记特定数据点或跟踪鼠标位置非常有用。 要使用 QCustomPlot游标功能,你需要完成以下步骤: 1. 在你的项目中添加 QCustomPlot 库。你可以从 CSDN 或 QCustomPlot 官方网站下载最新版本的库文件。 2. 在你的代码中创建一个 QCustomPlot 的实例: ```cpp QCustomPlot *plot = new QCustomPlot(this); ``` 3. 添加游标线到图表中。你可以使用 `addGraph` 函数创建一个游标线,并设置其属性,如颜色和线型: ```cpp QCPItemStraightLine *cursor = new QCPItemStraightLine(plot); cursor->setPen(QPen(Qt::red)); ``` 4. 在需要更新游标位置的事件中,如鼠标移动事件,通过设置游标线的位置来实时更新游标位置: ```cpp void YourWidget::mouseMoveEvent(QMouseEvent *event) { // 获取鼠标在图表上的坐标 QPointF mousePoint = plot->mapFromGlobal(event->globalPos()); // 设置游标线的位置 cursor->point1->setCoords(mousePoint.x(), plot->yAxis->range().lower); cursor->point2->setCoords(mousePoint.x(), plot->yAxis->range().upper); // 更新图表 plot->replot(); } ``` 这样,你就可以在 QCustomPlot 图表上实现游标功能了。记得根据你的需求,调整游标的样式和行为。希望这能帮到你!如果你还有其他问题,请随时继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值