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