使用Qt的QChartView实现缩放和放大功能

Qt的常用图表方式Qwt、QChart、QCustomPlot等。QCharts是官方的,功能强大些。QCustomPlot是一个小型的Qt画图标类,支持绘制静态曲线、动态曲线、多重坐标曲线,柱状图,蜡烛图等。QCustomPlot比Qchat简单好用些。

这里介绍下QChartView缩放和放大功能的实现。

这里介绍下QChartView缩放和放大功能的实现。

QChartView开启鼠标拖动放大功能:

ui->wdChart->setRubberBand(QChartView::RectangleRubberBand);

开启后,拖动鼠标区域自动放大,鼠标右键点击自动缩小。

恢复的话重新设置下轴的最大最小范围setRange即可。这里介绍下鼠标左键单击实现恢复的办法:

自定以一个MyChartView,继承自QChartView。增加一个信号:

signals:
  void sgl_recoverRange(MyChartView *p);

需要在自定义的MyChartView中区分出来是否是鼠标左键的单击事件还是鼠标左键的拖动。这里巧妙实现了下。原理很简单,如果是鼠标拖动的话mouseMoveEvent中把is_Pressed_清掉。

#include "mychartview.h"

MyChartView::MyChartView(QWidget *parent):QChartView(parent)
{
  is_Pressed_ = false;
}

void MyChartView::mouseMoveEvent(QMouseEvent *event)
{
  is_Pressed_ = false;
  QChartView::mouseMoveEvent(event);
}

void MyChartView::mouseReleaseEvent(QMouseEvent *event)
{
  if(event->button() == Qt::LeftButton){
      if(is_Pressed_){
          is_Pressed_ = false;
          // 单击鼠标恢复缩放
          emit sgl_recoverRange(this);
      }
  }
  QChartView::mouseReleaseEvent(event);
}

void MyChartView::mousePressEvent(QMouseEvent *event)
{
  is_Pressed_ = true;
  QChartView::mousePressEvent(event);
}

绑定信号和槽函数:

connect(ui->wdChart,&MyChartView::sgl_recoverRange,
       this,&MainWindow::slot_recoverChartRange);
m_tooltip = new Callout(myChart);

在槽函数中对缩放和放大功能进行恢复处理,重新设置range.

void MainWindow::slot_recoverChartRange()
{
  qDebug() << "slot_recoverChartRange";
  int maxVal = 0;
  if(mTbData.recList.size() == 0){
      mAxisY->setRange(0,12);
      mAxisY1->setRange(0,12);
      mAxisX->setRange(0,10);    
      return;
  }
}

 

更好用的QCustomPlot

QCustomPlot介绍

QCustomPlot 是一个基于Qt的画图和数据可视化C++控件。QCustomPlot 致力于提供美观的界面,高质量的2D画图、图画和图表,同时为实时数据可视化应用提供良好的解决方案。 该绘图库专注于制作美观、出版物质量高的2D绘图、图形和图表,并为实时可视化应用程序提供高性能。

QCustomPlot的下载与安装

QCustomPlot官网链接:Qt Plotting Widget QCustomPlot - Introduction
下载链接:Qt Plotting Widget QCustomPlot - Download

QCustomPlot的使用

QCustomPlot 是一个超强超小巧的qt绘图类,非常漂亮,非常易用。只需要把下载下来的qcustomplot.h和qcustomplot.cpp文件加入项目工程即可使用,远比qwt方便和漂亮,可以自己使用两个源文件也可以自己编译成库文件,非常方便。

把qcustomplot.cpp和qcustomplot.h拷贝到工程目录下,在项目中点右键添加现有文件,把两个文件加入工程。

这时pro文件会添加上qcustomplot.cpp和qcustomplot.h,如果Qt版本在5.0以上,需要在.pro文件中的QT变量加上printsupport,QT+= printsupport。

界面上拖上去一个widget控件,然后使一个widget提升为QCustomPlot类,即可使用。

使用示例

void OneGraph::OneGraph_Drawing(QCustomPlot *customPlot)
{
  // 将数据用曲线实时显示
  QVector<double> x(101),y(101);// x轴数据,y轴数据

  for(int i = 0; i < 101;i++)
  {
      x[i] = i / 50.0 - 1;// x轴数据范围:-1 ~ 1
      y[i] = x[i] * x[i];// y轴数据范围:0 ~ 1
  }

  // 添加一个曲线图QGraph,
  customPlot->addGraph();
  customPlot->graph(0)->setData(x,y);//为坐标轴添加数据
  customPlot->graph(0)->setName("示例1:绘制一个曲线");// 设置曲线图的名字

  // 如果需要添加多个曲线,就需要多次调用addGraph()函数
//    customPlot->addGraph();
//    customPlot->graph(1)->setData("x轴数据","y轴数据");
//    customPlot->graph(1)->setName("示例1:绘制第二个一个曲线");

  // 设置图表标题
  QCPTextElement *title = new QCPTextElement(customPlot,"标题:绘制一个曲线",QFont("sans",10,QFont::Bold));
  title->setTextColor(Qt::green);
  title->setMargins(QMargins(0,6,0,10));
  // 在第一行第一列添加标题
  customPlot->plotLayout()->insertRow(0);// 插入一行
  customPlot->plotLayout()->addElement(0, 0, title);

  //为图例添加标题
  QCPTextElement *legend_title = new QCPTextElement(customPlot,"这是图例的标题",QFont("sans",10,QFont::Bold));
  legend_title->setTextColor(Qt::red);
  legend_title->setMargins(QMargins(0,6,0,10));// 为了效果更好,添加一些边距
  legend_title->setLayer("legend");// 一定要把标题的层设置为legend层
  customPlot->legend->insertRow(0);// 插入一行
  customPlot->legend->addElement(0,0,legend_title);// 在第一行第一列添加标题

  // x轴设置属性
  customPlot->xAxis->setLabel("x轴数据");// 设置x轴的标签
  customPlot->xAxis->setRange(-1,1);// 设置x轴的范围为(-1,1)
  customPlot->xAxis->setPadding(30);//设置外边距,数值可以改大或者改小来观察效果
  customPlot->xAxis->setLabelPadding(20);//设置标签内边距
  customPlot->xAxis->setTickLabelPadding(10);

  // y轴设置属性
  customPlot->yAxis->setLabel("y轴数据");
  customPlot->yAxis->setRange(-1,1);
  customPlot->yAxis->setPadding(10);

  //设置QCustomPlot的背景颜色
  QLinearGradient plotGradient;
  plotGradient.setStart(0,0);//背景颜色起始点,从图左上角开始,y方向0~400之间为红色渐变,开始位置为红色
  plotGradient.setFinalStop(0,400);//y方向 >400 为绿色渐变,结束位置为绿色
  plotGradient.setColorAt(0,QColor(200,200,200));//黑色,透明度从 0 ~ 1,
  plotGradient.setColorAt(1,QColor(120,120,120));
  customPlot->setBackground(plotGradient);

  //设置QCPAxisRect轴矩形的背景颜色
  QLinearGradient axisRectGradient;
  axisRectGradient.setStart(0,0);
  axisRectGradient.setFinalStop(0,350);
  axisRectGradient.setColorAt(0,QColor("#87CEFA"));//亮天蓝色
  axisRectGradient.setColorAt(1,QColor("#FFB6C1"));//浅粉红
  customPlot->axisRect()->setBackground(axisRectGradient);

  //设置QCPAxis轴的风格
  customPlot->xAxis->setBasePen(QPen(Qt::white,2));// x轴线的画笔颜色和粗细
  customPlot->xAxis->setTickPen(QPen(Qt::white,3));// x轴线上的主刻度线(有数字的刻度线)的画笔颜色和粗细
  customPlot->xAxis->setTickLabelColor(Qt::green);// x轴线上的主刻度线下的文字颜色
  customPlot->xAxis->setTickLengthIn(6);// 轴线内主刻度线的长度
  customPlot->xAxis->setTickLengthOut(15);// 轴线外主刻度线的长度
  customPlot->xAxis->setSubTickPen(QPen(QColor(220,20,60),1));//粉红色,x轴线上的子刻度线(有数字的刻度线)的画笔颜色和粗细
  customPlot->xAxis->setLabelColor(Qt::red);// 只有设置了标签,轴标签的颜色才会显示
  customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);// 设置轴线结束时的风格为 实角三角形但内部有凹陷的形状
  customPlot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//setLowerEnding设置轴线开始时的风格

  //设置QCPGrid网格的风格,每条网格对应一个刻度
  customPlot->xAxis->grid()->setPen(QPen(QColor(255,0,255),1,Qt::SolidLine));//实线
  customPlot->yAxis->grid()->setPen(QPen(QColor(255,0,255),2,Qt::DotLine));//点线
  customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine)); // 子网格线(对应子刻度)画笔
  customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::SolidLine));//设置颜色后,需要显示子网格线,才能看到效果
  customPlot->xAxis->grid()->setSubGridVisible(true);// 显示子网格线
  customPlot->yAxis->grid()->setSubGridVisible(true);
  customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::black,3));// 设置刻度为0时的网格线的画笔
  customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::black,3));

  customPlot->legend->setVisible(true);// 显示图例:曲线颜色和曲线名,一般在右上角,可以设置

//    customPlot->replot();重绘
}

关于使用Qt的QChartView实现缩放和放大功能的文章就介绍至此,更多相关QtQChartView缩放和放大内容请搜索编程宝库以前的文章,希望以后支持编程宝库

 引入对于普通类型的对象来说,他们之间的复制很简单:int a = 10;int b = a;但是对于类对象来说,其中会存在许多的成员变量。#include <iostream>usi ...