QT5 QCustomPlot实现动态曲线绘制,可以左键放大、右键拖拽、跟随鼠标显示坐标

3 篇文章 0 订阅

QT5 QCustomPlot实现动态曲线绘制

1.准备

  1. 下载文件,官网:https://www.qcustomplot.com/
  2. 按照官网教程,qt添加帮助文件。
  3. git或github下载:XCustomPlot
  4. 打开项目,将1下载的文件解压,添加qcustomplot.cpp/p。
  5. 在pro文件中添加QT += widgets printsupport、添加CONFIG += c++11.
  6. 引入头文件 #include “qcustomplot.h”
  7. 在ui中添加Widgeet,提升为QCustomPlot;
  8. 编译。

2.鼠标矩形框进行框选放大、右键平移

可参考:https://blog.csdn.net/qq_31073871/article/details/90108646

1.在QCustomPlot类中添加private变量:

private:
  QRubberBand *rb;
  QPoint startPos;
  bool cancelRb;
  1. 直接在h文件中,QCustomPlot类中添加ESC按键处理函数
virtual void QCustomPlot::keyPressEvent(QKeyEvent *e)
{
    if(e->key() == Qt::Key_Escape) {
        cancelRb = true; //记录标志位,以便释放鼠标后不执行缩放代码
        rb->hide();//隐藏选择框
    }
}
  1. 在QCustomPlot的3个鼠标事件函数添加代码

0、在构造函数QCustomPlot::QCustomPlot(QWidget *parent)的初始化列表中添加:
  ,rb(new QRubberBand(QRubberBand::Rectangle, this))
  ,startPos(0, 0)
1、左键按下时,记录坐标起点
    在QCustomPlot::mousePressEvent(QMouseEvent *event)中添加:
    if(event->buttons() & Qt::LeftButton)
    {
        startPos = event->pos();
        cancelRb = false;
        rb->resize(0, 0);
        rb->show();
    }
 
2、左键按下并移动时,绘制矩形框
    在void QCustomPlot::mouseMoveEvent(QMouseEvent *event)中添加:
 
    if(event->buttons() & Qt::LeftButton)
    {
        QRect normalRect = QRect(startPos, event->pos()).normalized();//任意两点定义矩形
        rb->setGeometry(normalRect);
    }
3、左键弹起时,记录终点坐标,并把曲线放大到【起点、终点】围成的矩形框中
    在void QCustomPlot::mouseReleaseEvent(QMouseEvent *event)中添加:
 
    if(event->button() == Qt::LeftButton)
    {
        rb->hide();
        if(!cancelRb)
        {
            QRect normalRect = QRect(startPos, event->pos()).normalized();
            rb->setGeometry(normalRect);
            this->xAxis->setRange(xAxis->pixelToCoord(normalRect.left()),
                                  xAxis->pixelToCoord(normalRect.right()));
 
            this->yAxis->setRange(yAxis->pixelToCoord(normalRect.bottom()),
                                  yAxis->pixelToCoord(normalRect.top()));
            this->replot();//立即刷新图像
        }
    }
  1. 在void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details),内部改为右键
  if ((event->buttons() & Qt::RightButton) || (event->buttons() & Qt::MiddleButton))

3.最终效果

演示

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是如何在使用QCustomPlot绘制图表时实现鼠标左键框选放大鼠标右键拖动图表上下平移的功能,同时添加完整中文注释。 首先,QCustomPlot是一个强大的Qt绘图库,支持多种类型的图表绘制,例如线图、柱状图、散点图等等。在使用QCustomPlot绘制图表时,我们需要先创建一个QCustomPlot对象,并将其加入到QWidget中,然后通过调用QCustomPlot对象的函数来设置图表的各种属性,例如坐标轴范围、线条颜色等等。 接下来,我们来看如何实现鼠标左键框选放大鼠标右键拖动图表上下平移的功能。这里我们需要重载QCustomPlot对象的鼠标事件处理函数,以捕捉鼠标事件,然后根据不同的事件类型进行相应的处理。 具体实现方式如下: ```cpp #include <QCustomPlot> class MyCustomPlot : public QCustomPlot { public: MyCustomPlot(QWidget *parent = nullptr) : QCustomPlot(parent) { // 设置默认的鼠标交互模式为拖拽 setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); // 设置鼠标滚轮缩放 setInteraction(QCP::iRangeZoom, true); // 设置鼠标右键菜单 setContextMenuPolicy(Qt::CustomContextMenu); connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint))); } protected: void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { // 鼠标左键按下,记录起始点 m_startPos = event->pos(); } else if (event->button() == Qt::RightButton) { // 鼠标右键按下,记录起始位置 m_lastPos = event->pos(); } QCustomPlot::mousePressEvent(event); } void mouseReleaseEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton && event->pos() != m_startPos) { // 鼠标左键释放,计算选框的范围,并放大选框中的部分 QRect zoomRect = QRect(m_startPos, event->pos()).normalized(); zoomIn(zoomRect); } else if (event->button() == Qt::RightButton) { // 鼠标右键释放,计算拖动的距离并平移图表 QPoint delta = event->pos() - m_lastPos; scroll(delta.x(), delta.y()); } QCustomPlot::mouseReleaseEvent(event); } private slots: void contextMenuRequest(QPoint pos) { QMenu *menu = new QMenu(this); menu->addAction(tr("重置缩放"), this, SLOT(resetZoom())); menu->popup(mapToGlobal(pos)); } private: QPoint m_startPos; // 记录鼠标左键按下的起始位置 QPoint m_lastPos; // 记录鼠标右键按下的起始位置 }; ``` 在上面的代码中,我们创建了一个名为MyCustomPlot的类,继承自QCustomPlot,并重载了鼠标事件处理函数。在鼠标按下事件中,我们记录了鼠标左键右键按下的起始位置;在鼠标释放事件中,如果是鼠标左键释放,我们计算选框的范围并调用zoomIn函数对选框中的部分进行放大;如果是鼠标右键释放,我们计算拖动的距离并调用scroll函数来平移图表;同时,在右键菜单中添加了一个“重置缩放”的选项,用于将图表恢复到初始状态。 最后,为了方便使用和维护,我们可以在代码中添加完整中文注释,以提高代码的可读性和易用性。 希望这样能够解决您的问题,如有不足之处,还请指出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值