您的位置:首页 > 编程语言 > Qt开发

QT5使用QCustomPlot绘制实时曲线

2016-08-04 22:31 756 查看

在嵌入式开发中常会收到收集数据并且绘制实时折线图的需求。在linux设备上QT似乎是常用的开发框架。而用QT绘制漂亮的实时曲线,通常有两种方法,一是QWT控件,二是QCustomPlot控件。由于QWT安装相当繁琐,于是笔者使用了QCustomPlot。

一、下载、解压QCustomPlot源码

QCustomPlot下载

1、下载完整版并解压,将文件夹里面的头文件qcustomplot.h和源文件qcustomplot.cpp复制粘贴到工程文件夹下。然后在新建的QT工程中添加这两个文件。

2、在工程的.pro文件的第9行末尾加入printsupport,即

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport


二、在ui中添加QCustomPlot

1、进入ui设计模式,输入widget查找,从搜索结果中.向主窗口中添加一个widget区域,对着所添加的widget区域点击右键,选择“提升为”按钮。

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

3、选中QCustomPlot,点击提升按钮,我们创建的widget就被提升为QCustomPlot类了。

4、接着可以编译一下,若出现undefined reference的错误,请检查第一步的第二点

PS:提升可以理解为:widget是GUI的基本组件,控件如QCustomPlot是widget的“强化版”。

三、开始(愉快地)使用QCustomPlot

解释几个关键的成员函数:

addGraph()  //添加一条曲线
graph(x)->setPen(QPen(Qt::red));    //x是曲线序号,添加的第一条是0,设置曲线颜色
graph(x)->setData(x,y); //输出各点的图像,x和y都是QVector类
//其原型如下:
//void QCPGraph::setData(const QVector<double> &key, const QVector<double> &value)

xAxis->setLabel("x");   //x轴的文字
yAxis->setLabel("y");   //y轴的文字
CustomPlot->xAxis->setRange(0,11);  //x轴范围
CustomPlot->yAxis->setRange(0,11);  //y轴范围

replot();   //重绘


四、在QtCreator中使用帮助

为了更好地了解QCustomPlot的使用,可以将它的帮助文档放入qt creator中。

1、在先前解压的文件夹下有个documentation文件夹,其目录下有个qcustomplot.qch文件,在QtCreator ,工具,选项,帮助,文档,添加,选择qcustomplot.qch文件,确定

2、以后在其相关的函数或变量上按F1就能跳转到QCustomPlot的帮助文档了。

五、实现实时曲线

笔者是在linux上,使用qt5.7和QCustomPlot1.3.2做的测试

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qcustomplot.h"
//#include <QVector>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
double num[10];
int n;
void Graph_Show(QCustomPlot *customPlot);

public slots:
void Graph_Show();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QTime>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

for(int i=0;i<10;i++)
{
num[i] = 0;
}
n=0;
QTimer *timer = new QTimer(this);
timer->start(500);
connect(timer,SIGNAL(timeout()),this,SLOT(Graph_Show()));
}

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

void MainWindow::Graph_Show()
{

QTime t;
t=QTime::currentTime();
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
n=qrand()%50;
Graph_Show(ui->widget);
}

void MainWindow::Graph_Show(QCustomPlot *CustomPlot)
{
QVector<double> temp(10);
QVector<double> temp1(10);

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

CustomPlot->xAxis->setLabel("t");
CustomPlot->yAxis->setLabel("mV");

CustomPlot->xAxis->setRange(0,10);
CustomPlot->yAxis->setRange(-50,50);
CustomPlot->replot();
}


main.h不变

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

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

return a.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt 开发框架 控件