QCustomPlot使用心得一:安装和使用

6 篇文章 63 订阅

QCustomPlot是一个基于Qt C++的图形库,用于绘制和数据可视化 - 制作漂亮的2D图 - 曲线图、趋势图、坐标图、柱状图等。

有详细的例程和帮助文档,使用方便。

一、下载

到官网https://www.qcustomplot.com/index.php/download下载最新版本

 

二、安装帮助文档

1.下载后解压,有如下文档

(1)帮助文档:documentation

(2)例程:examples

(3)源文件:qcustomplot.cpp和qcustomplot.h

2.将帮助文档qcustomplot.qch,复制到QT目录下,例如E:\Qt\Docs\Qt-5.13.0(根据自己QT的安装位置)

3.打开QT,工具->选项->帮助->添加

4.选择qcustomplot.qch,帮助文档就安装好了

 

三、测试

1.新建工程,customplot.cpp和qcustomplot.h复制到工程文件里,并在项目里qcustomplot.cpp和qcustomplot.h添加进来,.pro里添加

QT       += printsupport

2.打开.ui文件,拖入一个widget控件,右键提升

3.输入QcustomPlot,点添加,再点提升,编译一下,看有没错误。

4.在构造函数里添加测试代码

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

    QCustomPlot* customPlot  = ui->customPlot;
    // add two new graphs and set their look:
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
    customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
    customPlot->addGraph();
    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(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); // exponentially decaying cosine
      y1[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)
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);
    // make left and bottom axes always transfer their ranges to right and top axes:
    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
    // pass data points to graphs:
    customPlot->graph(0)->setData(x, y0);
    customPlot->graph(1)->setData(x, y1);
    // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
    customPlot->graph(0)->rescaleAxes();
    // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
    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:
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}

5.运行,没错误能看到如下波形。

  • 14
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
C知道: 当然可以!QCustomPlot是一个用于Qt框架的C++绘图库,它提供了丰富的绘图和数据可视化功能。下面是一个简单的教程,帮助你开始使用QCustomPlot: 1. 首先,你需要将QCustomPlot添加到你的Qt项目中。你可以从QCustomPlot的官方网站(https://www.qcustomplot.com/)上下载最新版本的源码,然后将它添加到你的项目中。 2. 在你的Qt项目中包含QCustomPlot的头文件,并在代码中创建一个QCustomPlot对象。例如: ```cpp #include <QCustomPlot> int main(int argc, char *argv[]) { QApplication a(argc, argv); QCustomPlot plot; // 继续编写你的代码... return a.exec(); } ``` 3. 在你的代码中,你可以使用QCustomPlot对象的成员函数来配置和绘制图形。例如,你可以使用QCustomPlot::addGraph()函数添加一个曲线图: ```cpp QCPGraph *graph = plot.addGraph(); graph->setData(x, y); // x和y是你的数据 ``` 4. 使用QCustomPlot对象的其他成员函数来设置坐标轴、标题、图例等。例如,你可以使用QCustomPlot::xAxis 和 QCustomPlot::yAxis 函数来获取X轴和Y轴对象,并设置它们的标签: ```cpp QCPAxis *xAxis = plot.xAxis; xAxis->setLabel("X轴"); QCPAxis *yAxis = plot.yAxis; yAxis->setLabel("Y轴"); ``` 5. 最后,你需要调用QCustomPlot对象的replot()函数来更新绘图。这会触发绘图事件并将图形绘制到屏幕上: ```cpp plot.replot(); ``` 当然,以上只是QCustomPlot的一小部分功能。你可以查阅QCustomPlot的文档以了解更多可用的功能和选项。希望这个简单的教程能帮到你!如果还有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哀歌与世无争

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值