当前位置: 移动技术网 > IT编程>开发语言>C/C++ > QT QcustomPlot的简单使用

QT QcustomPlot的简单使用

2019年08月05日  | 移动技术网IT编程  | 我要评论

女子被骗30万遭羞辱,吕梁新闻,女服务员丝袜

第一步、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“。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网