都上大三了,第一次写博客。简单记录一下这两天的经历与收获。前两天打算用Qt做一个温湿度采集的动态折线图,一直没有找到合适下手的地方,用QPainter编程,感觉还是比较复杂,然后就在网上找各种方法。先是找到了qwt编写。但是安装就比较繁琐,还要各种版本相互对应,否则,安装很有可能不成功。然后再找,就找到了QCustomPlot,总的来说使用它,感觉比较方便。

QCustomPlot官方网站 :http://www.qcustomplot.com 

官方网站也有相应的教程和比较完成的样例源码。

下面我记录一些自己的第一个例程,希望能够帮助到初学的朋友。

首先到官网下载源码


解压并复制以下这两个文件,example里面有很多样例。

QCustomPlot第一个例程详细笔记_QT_02


打开Qt Creater建立工程


QCustomPlot第一个例程详细笔记_QT_05


QCustomPlot第一个例程详细笔记_QT_07QCustomPlot第一个例程详细笔记_QT_08


建立完工程后,添加刚才复制的两个文件到工程。


QCustomPlot第一个例程详细笔记_QT_11

然后打开界面文件 输入widget ,直接将其拖进去。如图所示。


QCustomPlot第一个例程详细笔记_QT_13

然后点击当刚才拖进去的控件,右键点击提升窗口控件,然后在提升类名称栏输入如图所示,然后点击添加,在上面提升的类选中刚才添加的,然后点击提升。


QCustomPlot第一个例程详细笔记_QT_15

然后点击运行,将会开到一个坐标系。


下面开始编写代码:

头文件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();
}

ok了,第一个例程完成。下面来看看效果吧。。。

QCustomPlot第一个例程详细笔记_QT_17