三味线
三味线
Published on 2018-12-10 / 74 Visits
0
0

QCustomPlot简单使用

下载

官网下载并解压:https://www.qcustomplot.com/

使用

  1. 创建一个QMainWindow应用,注意添加Print Support模块(.pro中Qt += printsupport);

  1. qcustomplot.hqcustomplot.cpp复制到项目目录,并添加到项目中;

  2. 添加代码如下:

#ifndef CPLOT0_H
#define CPLOT0_H
#include <QtWidgets/QMainWindow>
#include "ui_cplot0.h"
#include "qcustomplot.h"
class cplot0 : public QMainWindow
{
    Q_OBJECT
public:
    cplot0(QWidget *parent = 0);
    ~cplot0();
    void simpledemo(QCustomPlot *customPlot);
private:
    Ui::cplot0Class ui;
};
#endif // CPLOT0_H
#include "cplot0.h"
#include <QVector>
#include <QtMath>
cplot0::cplot0(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->setCentralWidget(ui.widget);
    simpledemo(ui.widget);
}
cplot0::~cplot0()
{
}
void cplot0::simpledemo(QCustomPlot *customPlot)
{
    QVector<double> x, y;
    for (double i=-10; i <= 10; i=i+0.1)
    {
        double x1 = i;
        double y1 = qSin(i) * 100;
        x.append(x1);
        y.append(y1);
    }
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::red));
    customPlot->graph(0)->setLineStyle(QCPGraph::lsImpulse);
    customPlot->graph(0)->setData(x, y);
    customPlot->xAxis->setLabel("time");
    customPlot->yAxis->setLabel("value");
    customPlot->xAxis->setRange(-10, 10);
    customPlot->yAxis->setRange(-100, 100);
}
  1. 效果:

  2. 参考官网的示例:https://www.qcustomplot.com/index.php/introduction


Comment