QCustomPlot实现矩形区域缩放图表以及修改为右键拖动

2024-01-12 01:59

本文主要是介绍QCustomPlot实现矩形区域缩放图表以及修改为右键拖动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

右键拖动

Ctrl+F搜索void QCPAxisRect::mousePressEvent(QMouseEvent *event)

函数圆形如下:

把Qt::LeftButton改为Qt::RightButton

void QCPAxisRect::mousePressEvent(QMouseEvent *event)
{
  mDragStart = event->pos(); // need this even when not LeftButton is pressed, to determine in releaseEvent whether it was a full click (no position change between press and release)
  if (event->buttons() & Qt::Left)// 鼠标右键移动
  {
    mDragging = true;
    // initialize antialiasing backup in case we start dragging:
    if (mParentPlot->noAntialiasingOnDrag())
    {
      mAADragBackup = mParentPlot->antialiasedElements();
      mNotAADragBackup = mParentPlot->notAntialiasedElements();
    }
    // Mouse range dragging interaction:
    if (mParentPlot->interactions().testFlag(QCP::iRangeDrag))
    {
      if (mRangeDragHorzAxis)
        mDragStartHorzRange = mRangeDragHorzAxis.data()->range();
      if (mRangeDragVertAxis)
        mDragStartVertRange = mRangeDragVertAxis.data()->range();
    }
  }
}

意图

我们知道图表有些基本的功能,比如放大(ZoomIn)、缩小(ZoomOut)、拖拽(Drag)、恢复(1:1或ZoomReset)。这些功能QCustomPlot已经几乎都有了,但是选择矩形区域进行缩放的功能没有给出很好的接口,这可能就要我们自己实现了,这就是写这篇文章的目的。


实现

QCustomPlot本身有缩放的功能,但是没有实现矩形缩放的接口,所以实现的话无非是在图表上面实现鼠标选择的矩形区域,然后进行比例缩放。我们借助Qt自带的QRubberBand来实现矩形选择区域。看帮助文档和源码,我们可以看到当鼠标在图表区域上的时候,包括鼠标按下,移动,释放,这些动作会发送信号。所以有两种方法实现,第一种方法我们只要以父控件实现槽函数来绑定这些信号来做特定处理就行了。第二种方法实现QCustomPlot的子类,重载鼠标按下,移动,释放等鼠标事件函数。


父控件槽函数实现方法

按照QCustomPlot提供的例子,我们写一个最简单的例子。
把QCustomPlot设为MainWindow的centralWidget。

UI界面

少废话了,上源码吧:
MainWindow的头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QRubberBand>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();
private slots:void mousePress(QMouseEvent* mevent);void mouseMove(QMouseEvent *mevent);void mouseRelease(QMouseEvent *mevent);void slotBtn();private:Ui::MainWindow *ui;QRubberBand *rubberBand;QPoint rubberOrigin;
};#endif // MAINWINDOW_H

MainWindow的构造函数实现

//初始化QRubberBand
rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->customPlot);//从自带的例子中复制的代码
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
ui->customPlot->addGraph();
ui->customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(250), y0(250), y1(250);
for (int i=0; i<250; ++i)
{x[i] = i;y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosiney1[i] = qExp(-i/150.0);              // exponential envelope
}
// configure right and top axis to show ticks but no labels:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// pass data points to graphs:
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);
// let the ranges scale themselves so graph 0 fits perfectly in the visible area:
ui->customPlot->graph(0)->rescaleAxes();
// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
ui->customPlot->graph(1)->rescaleAxes(true);
// Note: we could have also just called customPlot->rescaleAxes(); instead
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);//连接鼠标事件发出的信号,实现绑定
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));//实现恢复1:1功能
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotBtn()));
//鼠标按下槽函数
void MainWindow::mousePress(QMouseEvent* mevent)
{if(mevent->button() == Qt::RightButton){rubberOrigin = mevent->pos();rubberBand->setGeometry(QRect(rubberOrigin, QSize()));rubberBand->show();}
}
//鼠标移动槽函数
void MainWindow::mouseMove(QMouseEvent *mevent)
{if(rubberBand->isVisible())rubberBand->setGeometry(QRect(rubberOrigin, mevent->pos()).normalized());
}
//鼠标释放槽函数
void MainWindow::mouseRelease(QMouseEvent *mevent)
{Q_UNUSED(mevent);if (rubberBand->isVisible()){const QRect zoomRect = rubberBand->geometry();int xp1, yp1, xp2, yp2;zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);double x1 = ui->customPlot->xAxis->pixelToCoord(xp1);double x2 = ui->customPlot->xAxis->pixelToCoord(xp2);double y1 = ui->customPlot->yAxis->pixelToCoord(yp1);double y2 = ui->customPlot->yAxis->pixelToCoord(yp2);ui->customPlot->xAxis->setRange(x1, x2);ui->customPlot->yAxis->setRange(y1, y2);rubberBand->hide();ui->customPlot->replot();}
}
//槽函数实现1:1恢复功能
void MainWindow::slotBtn()
{ui->customPlot->rescaleAxes();ui->customPlot->replot();
}

子类化QCustomPlot重载鼠标事件函数方法

我们继承QCustomPlot实现子类CustomPlotZoom类。
CustomPlotZoom.h头文件

#ifndef CUSTOMPLOTZOOM_H
#define CUSTOMPLOTZOOM_H#include <QPoint>
#include "qcustomplot.h"class QRubberBand;
class QMouseEvent;
class QWidget;class CustomPlotZoom : public QCustomPlot
{Q_OBJECTpublic:CustomPlotZoom(QWidget * parent = 0);virtual ~CustomPlotZoom();void setZoomMode(bool mode);protected:void mousePressEvent(QMouseEvent * event);void mouseMoveEvent(QMouseEvent * event);void mouseReleaseEvent(QMouseEvent * event);private:bool mZoomMode;QRubberBand * mRubberBand;QPoint mOrigin;
};#endif // CUSTOMPLOTZOOM_H

CustomPlotZoom构造函数实现

CustomPlotZoom::CustomPlotZoom(QWidget * parent): QCustomPlot(parent), mZoomMode(false), mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
{}

设置矩形缩放功能的使能

void CustomPlotZoom::setZoomMode(bool mode)
{mZoomMode = mode;
}

鼠标事件的实现

void CustomPlotZoom::mousePressEvent(QMouseEvent * event)
{if (mZoomMode){if (event->button() == Qt::RightButton){mOrigin = event->pos();mRubberBand->setGeometry(QRect(mOrigin, QSize()));mRubberBand->show();}}QCustomPlot::mousePressEvent(event);
}void CustomPlotZoom::mouseMoveEvent(QMouseEvent * event)
{if (mRubberBand->isVisible()){mRubberBand->setGeometry(QRect(mOrigin, event->pos()).normalized());}QCustomPlot::mouseMoveEvent(event);
}void CustomPlotZoom::mouseReleaseEvent(QMouseEvent * event)
{if (mRubberBand->isVisible()){const QRect zoomRect = mRubberBand->geometry();int xp1, yp1, xp2, yp2;zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);double x1 = xAxis->pixelToCoord(xp1);double x2 = xAxis->pixelToCoord(xp2);double y1 = yAxis->pixelToCoord(yp1);double y2 = yAxis->pixelToCoord(yp2);xAxis->setRange(x1, x2);yAxis->setRange(y1, y2);mRubberBand->hide();replot();}QCustomPlot::mouseReleaseEvent(event);
}

结果

右键来实现选择矩形区域,因为QCustomPlot左键实现了拖拽功能。

右键选择矩形区域

缩放结果

然后可以随便缩放,拖拽。然后点击ZoomReset按钮,就可以实现一键恢复到最初的样子,做到不忘初心。

 

这篇关于QCustomPlot实现矩形区域缩放图表以及修改为右键拖动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/596442

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import