qt绘制坐标系--利用QCustomPlot

来源:互联网 发布:手机ps软件 编辑:程序博客网 时间:2024/05/20 16:32

以下文章来自链接:http://www.cnblogs.com/yingjiehit/p/3988701.html 本文只为个人留作记录

一、QCustomPlot的安装

1 官方下载地址:http://www.qcustomplot.com/index.php/download

2 下载之后将文件解压到任意一个文件夹中,不要有中文路径。

3 打开QT,新建一个窗体工程

4 将解压得到的文件夹里面的头文件qcustomplot.h和源文件qcustomplot.cpp复制粘贴到工程文件夹下。

5 在Qt中,对着工程名右键,添加已有文件,将头文件qcustomplot.h和源文件qcustomplot.cpp都添加到工程中来。

6 接着在工程的pro文件的第9行末尾加入代 printsupport

7 打开界面文件,进入图形化设计界面,向主窗口中添加一个widget区域,对着所添加的widget区域点击右键,选择“提升为”按钮。

8 提升类名称输入“QCustomPlot”,点击添加。

9 在之后的界面中选中QCustomPlot,点击提升按钮,我们创建的widget就被提升为QCustomPlot类了。

10 现在我们运行一下程序,就会出现一个简单的坐标系了。

QCustomPlot的基本配置就做好了。

二、第一个例子

我们可以理解为QCustomPlot就是一个绘图板的类,它继承于Widget,界面中的Widget类提升为QCustomPlot才能够绘图。

QCustomPlot中的每一个曲线是一个Graph对象,凡是跟显示数据有关的我们就对Graph进行操作或调用Graph对象提供的方法。

一个QCustomPlot里有四个坐标轴,其中xAxisyAxis就是我们上图看到的x和y坐标轴,还有两个坐标轴xAxis1和yAxis1为上方和右方的x、y坐标,默认隐藏,可以通过程序设计显示。

具体绘图执行步骤:

首先我们将上面提升为QCustomPlot类的容器界面的对象重命名为qCustomPlot。

示例如下:

头文件mainwindow.h中添加

 #include "qcustomplot.h"

void SimpleDemo(QCustomPlot *customPlot); 

mainwindow.cpp文件中代码如下:

 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
//#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    SimpleDemo(ui->qCustomPlot);

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::SimpleDemo(QCustomPlot *CustomPlot)
{

    QVector<double> x(101),y(101);
    for(int i=0;i<101;i++)
    {
        x[i] = i/5.0-10;
        y[i] = x[i]*x[i]*x[i];
    }
    CustomPlot->addGraph();
    CustomPlot->graph(0)->setPen(QPen(Qt::yellow));
    CustomPlot->graph(0)->setData(x,y);



    QVector<double> temp(10);
    QVector<double> temp1(10);
    for(int i=0;i<10;i++)
    {
        temp[i] = i;
        temp1[i] = 100*i;
    }
    CustomPlot->addGraph();
    CustomPlot->graph(1)->setPen(QPen(Qt::red));
    CustomPlot->graph(1)->setData(temp,temp1);

    CustomPlot->xAxis->setLabel("time");
    CustomPlot->yAxis->setLabel("temp/shidu");

    CustomPlot->xAxis->setRange(-11,11);
    CustomPlot->yAxis->setRange(-1100,1100);

}

main.cpp中不变

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

 

main.cpp中不变

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

 

1 0