龙潭虎穴网

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

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){     // 将数据用曲线实时显示    QVectorx(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之QCustomPlot介绍_贝勒里恩的博客-CSDN博客_qcustomplot

QCustomPlot基础教程(一)——QCustomPlot的安装及基础实例_wendy_ya的博客-CSDN博客_qcustomplot

qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程_尘中远的博客-CSDN博客_qt绘图控件

qt超强绘图控件qwt - 安装及配置_尘中远的博客-CSDN博客

Qt 下快速读写Excel指南_尘中远的博客-CSDN博客_qt读写excel

Qt使用多线程的一些心得——2.继承QObject的多线程使用方法_尘中远的博客-CSDN博客

QCustomplot - 6.QCustomplot详解_Mr.codeee的博客-CSDN博客_qcustomplot

Qwt、QChart、QCustomPlot使用_mahuifa的博客-CSDN博客_qcustomplot和qchart对比

Qt-QCustomplot画静态、动态曲线教程图解_52_赫兹的鲸的博客-CSDN博客_qcustomplot曲线

QT图表:QChart\QCustomplot_rainbow_lucky0106的博客-CSDN博客_qcustomplot和qchart对比

Qt-Qcustomplot设置并使用双重坐标轴(刻度不同的X、Y轴)_52_赫兹的鲸的博客-CSDN博客

QCustomPlot使用_pzs0221的博客-CSDN博客_qcustomplot

QCustomPlot-用鼠标矩形框进行框选放大_宁静致远2021的博客-CSDN博客_qcustomplot框选放大 QCustomPlot绘图实现光标滑过曲线显示点的坐标_shaderdx的博客-CSDN博客_qcustomplot鼠标显示坐标

QCustomPlot使用心得六:框选放大,拖动,选中数据 - 阳光下的小土豆 - 博客园

使用QCustomPlot在数据曲线上设置可随鼠标移动的游标_weixin_45875835的博客-CSDN博客

未经允许不得转载:龙潭虎穴网 » Qt的QChartView实现缩放和放大功能