QCustomPlot的使用(1)

     公司要做一个采样波形的上位机,评估了QT几种绘图的图形库,最终选择QCustomplot,主要是由于精度高,刷新速率快,在这里简单介绍一下QCustomplot的使用
1.简介

QCustomplot是一个基于QT的图形库,QCustomPlot是一个小型的qt画图标类,效果可以,易用,只需要在项目中加入头文件 qcustomplot.h 和 qcustomplot.cpp 文件,然后使一个widget提升为QCustomPlot类,即可使用

2.使用

首先在官网下载 qcustomplot.h 和 qcustomplot.cpp 文件,官网中包含许多demo.

官网链接:Qt Plotting Widget QCustomPlot - Introduction

下载之后可以将使用文档qhp文件添加到QT中,可以详细查看相关类的使用说明,以及详细用法。

类似于上面截图这样,相关类和函数不明白的可以查看相关说明

3.一个demo

首先创建一个QT项目(在官方实例加了一些更改和注释)

在UI文件中添加一个widget,将qcustomplot.h和qcustomplot.cpp导入项目中,将widget提升为QCustomPlot,

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qcustomplot.h"


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void printToTwo(QCustomPlot *customPlot);//画两条线

    bool eventFilter(QObject *obj, QEvent *event); //事件检测器

private:
    Ui::MainWindow *ui;
    QCustomPlot *customPlot;
    QLabel *coordinateLabel;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStatusBar>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    customPlot = ui->widget;

    coordinateLabel = new QLabel();
    coordinateLabel->setText("点击图表查看坐标");
    //coordinateLabel->move(700, 300); // 假设将标签放置在窗口的左上角
 //   QStatusBar *status= statusBar();
   ui->statusbar->addWidget(coordinateLabel,1);


    printToTwo(customPlot);

    customPlot->installEventFilter(this);

}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == customPlot && event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        double x = customPlot->xAxis->pixelToCoord(mouseEvent->pos().x());
        double y = customPlot->yAxis->pixelToCoord(mouseEvent->pos().y());

        coordinateLabel->setText(QString("X: %1, Y: %2").arg(x).arg(y));
    }
    return QObject::eventFilter(obj, event);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::printToTwo(QCustomPlot *customPlot)
{
    //每条曲线都会独占一个graph()
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue)); // 曲线的颜色
    customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // 曲线与X轴包围区的颜色
    customPlot->addGraph();//添加graph等价于添加新曲线
    customPlot->graph(1)->setPen(QPen(Qt::red)); // 曲线的颜色

    // 生成模拟数据点 (x-y0 第一条曲线, x-y1为第2条曲线):
    QVector<double> x(251), y0(251), y1(251);
    for (int i=0; i<251; ++i)
    {
      x[i] = i;
      y0[i] = qExp(-i/150.0)*qCos(i/10.0); // 第一条曲线:y0衰减cos
      y1[i] = qExp(-i/150.0);              // 第二条曲线:y1衰减指数
    }
    
    // 边框右侧和上侧均显示刻度线,但不显示刻度值:
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

    // 使上下两个X轴的范围总是相等,使左右两个Y轴的范围总是相等
    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

    // 把已存在的数据填充进graph的数据区
    customPlot->graph(0)->setData(x, y0);
    customPlot->graph(1)->setData(x, y1);

    
    customPlot->graph(0)->rescaleAxes();
    customPlot->graph(1)->rescaleAxes(true);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    // 立即刷新图像
    ui->widget->replot();
}

运行结果:

缩放,相关选项都开启了

鼠标双击可以查看对应点坐标

QCustomPlot 是一个功能强大、灵活易用的开源绘图小部件库。它可以在Qt应用程序中创建可定制和交互式的绘图窗口。 使用手册详细讲解了QCustomPlot库的各种功能和用法。使用手册中的第一部分介绍了安装QCustomPlot,包括如何获取并集成库文件到项目中。第二部分解释了如何创建QCustomPlot实例,设置横轴和纵轴的范围,以及如何添加数据。手册提供了示例代码,方便开发者理解和使用。 手册的第三部分涵盖了各种绘图元素的绘制和自定义。开发者可以学习如何添加图表标题、轴标签和图例。手册还介绍了如何绘制折线图、散点图和柱状图,并解释了如何进行绘图属性的自定义,如颜色、样式和线宽等。 第四部分讲解了绘图交互和响应事件。开发者可以学习如何平移、缩放和选择绘图,以及如何处理各种交互事件,如鼠标点击和拖动。 最后,使用手册提供了一些附加功能和技巧。开发者可以学习如何添加数学和统计函数、图像贴图、自定义绘图样式和绘制函数曲线等高级功能。 总而言之,QCustomPlot使用手册提供了全面而详细的关于如何使用这个库的指导。通过学习和实践,开发者可以灵活使用QCustomPlot创建自定义和交互式的绘图窗口。无论是初学者还是有经验的开发者,都可以从这个手册中获得帮助,提升绘图应用程序的质量和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值