Qt开发:QCustomPlot

【官方网站】:Qt Plotting Widget QCustomPlot - Introduction

       QCustomPlot 是一个用于绘制图形的 Qt C++ 库,它使得在 Qt 程序中创建高质量的 2D 图表变得简单。这个库非常适合用来展示实时数据,同时它也支持多种输出格式,包括打印质量的矢量图形如 PDF 文件。

以下是 QCustomPlot 的一些主要特性:

  • 提供丰富的 2D 绘图类型,比如折线图、散点图、柱状图等。
  • 支持多轴图表和多图例。
  • 实时更新图表,适合用于显示不断变化的数据流。
  • 可以导出为多种图形格式,如 PNG, JPG, BMP 或者矢量图形格式如 PDF 和 SVG。
  • 完全可定制化,允许用户调整几乎所有的视觉方面,从字体到颜色,从线条样式到标记形状。
  • 内置的支持交互功能,如缩放和平移、轴调整和项目选择。
  • 文本元素可以使用 LaTeX 语法进行数学排版。
  • 开源免费:采用GPLv3许可,可免费用于商业项目
  • 轻量级:仅需一个头文件和一个源文件即可集成
  • 高性能:优化处理大数据集显示
  • 跨平台:支持Windows、Linux和macOS

核心类解析

QCustomPlot 的核心架构围绕几个主要类构建,以下是这些核心类的详细说明:

1. QCustomPlot (主绘图控件类)

功能:作为所有绘图操作的中央容器和接口

主要方法

  • addGraph() - 添加新图表

  • addPlottable() - 添加可绘制对象

  • rescaleAxes() - 自动调整坐标轴范围

  • replot() - 重绘整个图表

  • savePng()/savePdf() - 导出图像

关键属性

  • xAxis/yAxis - 底部和左侧坐标轴

  • xAxis2/yAxis2 - 顶部和右侧坐标轴

  • legend - 图例对象

2. QCPAxis (坐标轴类)

功能:管理坐标轴的显示和缩放

主要方法

  • setRange() - 设置显示范围

  • setScaleType() - 设置线性/对数比例

  • setLabel() - 设置轴标签

  • setTickLabels() - 控制刻度标签显示

重要信号

  • rangeChanged() - 轴范围变化时触发

3. QCPAbstractPlottable (可绘制对象基类)

派生类

  • QCPGraph - 折线图/曲线图

  • QCPCurve - 参数曲线

  • QCPBars - 柱状图

  • QCPStatisticalBox - 箱线图

  • QCPColorMap - 颜色映射图

通用方法

  • setData() - 设置绘图数据

  • setPen()/setBrush() - 设置线条/填充样式

  • setName() - 设置名称(显示在图例中)

4. QCPLegend (图例类)

功能:管理图表中各项的图例显示

主要方法

  • addItem() - 添加图例项

  • removeItem() - 移除图例项

  • setVisible() - 控制可见性

5. QCPLayout (布局系统)

派生类

  • QCPLayoutGrid - 网格布局

  • QCPLayoutInset - 内嵌布局

功能:管理绘图元素的位置和排列

6. QCPItem (绘图项基类)

派生类

  • QCPItemLine - 线段

  • QCPItemRect - 矩形

  • QCPItemText - 文本

  • QCPItemEllipse - 椭圆

  • QCPItemPixmap - 位图

通用方法

  • setPen()/setBrush() - 设置样式

  • setPosition() - 设置位置

使用示例

     展示一个完整的 QCustomPlot 使用示例,包含数据准备、图表绘制、样式设置和交互功能

#include 
#include 
#include "qcustomplot.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow window;
    
    // 1. 创建QCustomPlot控件
    QCustomPlot *customPlot = new QCustomPlot(&window);
    window.setCentralWidget(customPlot);
    window.resize(800, 600);
    window.setWindowTitle("QCustomPlot Demo");
    
    // 2. 准备数据
    QVector x(100), y(100);
    for (int i = 0; i < 100; ++i) {
        x[i] = i / 10.0;  // 0到9.9,步长0.1
        y[i] = qSin(x[i]); // 正弦函数
    }
    
    // 3. 创建图表并设置数据
    customPlot->addGraph();
    customPlot->graph(0)->setData(x, y);
    
    // 4. 设置图表样式
    customPlot->graph(0)->setName("SinCurve");
    customPlot->graph(0)->setPen(QPen(Qt::blue, 2)); // 蓝色线条,宽度2
    customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // 半透明蓝色填充
    
    // 5. 设置坐标轴
    customPlot->xAxis->setLabel("X");
    customPlot->yAxis->setLabel("Y");
    customPlot->xAxis->setRange(0, 10); // X轴范围0-10
    customPlot->yAxis->setRange(-1.5, 1.5); // Y轴范围-1.5到1.5
    
    // 6. 显示图例
    customPlot->legend->setVisible(true);
    customPlot->legend->setBrush(QBrush(QColor(255,255,255,150))); // 半透明白色背景
    
    // 7. 添加标题
    customPlot->plotLayout()->insertRow(0); // 在上方插入空行
    QCPTextElement *title = new QCPTextElement(customPlot, "Sin", QFont("宋体", 12, QFont::Bold));
    customPlot->plotLayout()->addElement(0, 0, title);
    
    // 8. 添加交互功能
    // 8.1 支持拖动和缩放
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    // 8.2 显示数据点提示
    QObject::connect(customPlot, &QCustomPlot::mouseMove, [=](QMouseEvent *event) {
        double x = customPlot->xAxis->pixelToCoord(event->pos().x());
        double y = customPlot->yAxis->pixelToCoord(event->pos().y());
        customPlot->setToolTip(QString("X: %1\nY: %2").arg(x, 0, 'f', 2).arg(y, 0, 'f', 4));
    });
    
    // 9. 重绘图表
    customPlot->replot();
    
    window.show();
    return a.exec();
}

 绘制呈现

Qt开发:QCustomPlot_第1张图片

    这些核心类共同构成了QCustomPlot强大的绘图功能体系,通过它们的组合可以实现复杂的数据可视化需求。

高级功能

  • 多坐标轴支持(顶部/右侧坐标轴)

  • 多种图表类型(折线图、散点图、柱状图、统计图、金融图)

  • 图例和颜色标尺

  • 实时数据可视化

  • 导出为PNG、PDF、JPG、BMP等格式

  • 文本标签、曲线、箭头等绘图元素

你可能感兴趣的:(qt,QCustomPlot)