QCustomPlot实现动态折线图

 话不多说,QCustomPlot第二弹,实现动态折线图。
需要注意以下几点:
1、
//Qt中setData原型
void QCPGraph::setData(const QVector<double> &key, const QVector<double> &value) 
QVector类是一个提供动态数组的模板类。QVector不能插入、添加、替换一个QVector,否则你的应用程序就会报错!
解决方法:定义临时变量。然后定义一个全局变量通过形参的方式,传入参数中。
2、
怎么样让折线图的数据变化,当然是通过产生随机数了,以下是Qt产生随机数的两种方法。

Qt中生成随机数

方式一:

#include <QTimer>

QTime t;

t= QTime::currentTime();

 qsrand(t.msec()+t.second()*1000);

int n = qrand();

方式二:

#include <QTimer>

#include<time.h>

qsrand(time(NULL));

int n = qrand();

以下是源代码:

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:
    double tempnum[10];
    int n;
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
   void SimpleDemo(QCustomPlot *customPlot,double tempnum[10],int i);
public slots:
    void SimpleDemo();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QTimer>
#include <QTime>
//#include <time.h>
//#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i=0;i<10;i++)
    {
        tempnum[i] = 0;
    }
    n=0;
    QTimer *timer = new QTimer(this);
    timer->start(2000);
     connect(timer,SIGNAL(timeout()),this,SLOT(SimpleDemo()));
}

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


void MainWindow::SimpleDemo()
{

    QTime t;
    t=QTime::currentTime();
    qsrand(t.msec()+t.second()*1000);
    n=qrand()%50+5;
    SimpleDemo(ui->qCustomPlot,tempnum,n);
}


void MainWindow::SimpleDemo(QCustomPlot *CustomPlot,double tempnum[10],int i)
{
    QVector<double> temp(10);
    QVector<double> temp1(10);

     for(int i=8;i>=0;i--)
    {
        tempnum[i+1]=tempnum[i];
    }
    tempnum[0]=n;
    for(int i=0;i<10;i++)
    {

        temp[i] = i;
        temp1[i] =tempnum[i];
    }

    CustomPlot->addGraph();
    CustomPlot->graph(0)->setPen(QPen(Qt::red));
    CustomPlot->graph(0)->setData(temp,temp1);

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

    CustomPlot->xAxis->setRange(0,11);
    CustomPlot->yAxis->setRange(0,100);
    CustomPlot->replot();
}
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
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃果冻不吐果冻皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值