QT QcustomPlotの簡単な使用

最初のステップは、QcustomPlotは、サードパーティのライブラリのQTを提供することで、あなたは使用前にQcustomPlotで公式サイトをダウンロードする必要があります。

第二のステップは、プロジェクトへのファイルに追加し、完成QcustomPlot圧縮パッケージqcustomplot.hとqcustomplot.cppファイルを抽出します。これら二つの文書に追加し、既存のファイルを追加しますクリックするだけで、ソースファイルで最初に使用する必要があります。

 

 

第三段階、UIインターフェイスを開き、インターフェイスにweigetコントロールを追加し、コントロールを右クリックして、選択アップグレード

クラス名のリフティングにQcustomPlotを書いて、アップグレードします。

このようなQcustomPlotこのサードパーティのライブラリを使用することができます。

以下は、単純な曲線コードです。

.cppファイル

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置鼠标点击精度
    ui->customPlot->setSelectionTolerance(1);

    for(int i=0;i<20;i++)
    {
        num[i]=0;
    }
    n=0;
    QTimer *t = new QTimer(this);
    t->start(500);
    connect(t,SIGNAL(timeout()),this,SLOT(graph_show()));
    connect(ui->customPlot,SIGNAL(mouseRelease(QMouseEvent*)),this,SLOT(mouseReleaseEvent(QMouseEvent*)));
    //connect(tracer,SIGNAL(mouseMove(QMouseEvent*)),this,SLOT(mouseMoveEvent(QMouseEvent*)));

}

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

void MainWindow::graph_show()
{
    n += PI/8;
    graph_show(ui->customPlot);
}


void MainWindow::graph_show(QCustomPlot *customPlot)
{

    QVector<double> x(20),y(20);
    for(int i=0;i<19;i++)
    {
        num[i]=num[i+1];
    }
    num[19]=n;
    for(int i=0;i<20;i++)
    {
        x[i] = i;
        y[i] = sin(num[i]);
    }
    //添加一条曲线
    customPlot->addGraph();
    //设置曲线的颜色
    customPlot->graph(0)->setPen(QPen(Qt::red));
    //给曲线传递两个参数
    customPlot->graph(0)->setData(x,y);
    //给曲线的横纵坐标命名
    customPlot->xAxis->setLabel("x");
    customPlot->yAxis->setLabel("y");
    //设置横纵坐标的范围
    customPlot->xAxis->setRange(0,20);
    customPlot->yAxis->setRange(-3,3);
    //进行曲线重画
    customPlot->replot();
    /*
    customPlot->setInteraction(QCP::iRangeZoom,true);
    customPlot->axisRect()->setRangeDrag(Qt::Vertical);
    customPlot->setInteraction(QCP::iRangeDrag,true);
    */
}

void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    //排除非左鼠标键
    if (e->button() != Qt::LeftButton)
    {
        return;
    }

    //获取点击的点坐标
    QPointF ChickedPoint = e->pos();
    //排除区间外鼠标点
    if(!ui->customPlot->viewport().contains(e->pos()))
    {
        return;
    }
    //将像素坐标转换为轴值
    double currentx = ui->customPlot->xAxis->pixelToCoord(ChickedPoint.x());
    double currenty = ui->customPlot->yAxis->pixelToCoord(ChickedPoint.y());
    //使用QToolTip输出值,
    QToolTip::showText(mapToGlobal(e->pos()),QString("当前点值为:x=%1,y=%2").arg(currentx).arg(currenty),this);
}

.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"
#include <QMouseEvent>

#define PI 3.1415926

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    //设置一容器
    double num[20];
    double n=0;
    void graph_show(QCustomPlot *customPlot);

public slots:
    void graph_show();
    void mouseReleaseEvent(QMouseEvent *e);
//     void mouseMoveEvent(QMouseEvent *e);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

静态曲线的命名方法可以选用:

    customPlot->legend->setVisible(true);
    customPlot->graph(0)->setName("sin");

此处是对第一条曲线进行命名为“sin“。

おすすめ

転載: www.cnblogs.com/caozewen/p/11289842.html
おすすめ