Qt4中,可以使用QCustompPlot来绘制曲线,QCustompPlot是一个第三方工具,可以到官网下载:http://www.qcustomplot.com/index.php/download
这里实现一个实时动态曲线图,用随机数作为实时数据,程序运行结果如下:
主机环境:fedora9,Qt4.7,Qtcreator 2.0.1
使用Qtcreator 2.0.1新建一个工程,基类模板选择QMainWindow。 将解压得到的QCustompPlot文件夹里面的头文件qcustomplot.h和源文件qcustomplot.cpp复制粘贴到工程文件夹下。 在Qtcreator中,对着工程名右键,添加已有文件,将头文件qcustomplot.h和源文件qcustomplot.cpp都添加到工程中来。
在界面上拖拽一个widget部件,然后升级成Qcustomplot,(参考:http://www.bubuko.com/infodetail-744744.html)部件名称改为customPlot
mainwindow.h代码如下:
mainwindow.cpp代码如下:
这里实现一个实时动态曲线图,用随机数作为实时数据,程序运行结果如下:
主机环境:fedora9,Qt4.7,Qtcreator 2.0.1
使用Qtcreator 2.0.1新建一个工程,基类模板选择QMainWindow。 将解压得到的QCustompPlot文件夹里面的头文件qcustomplot.h和源文件qcustomplot.cpp复制粘贴到工程文件夹下。 在Qtcreator中,对着工程名右键,添加已有文件,将头文件qcustomplot.h和源文件qcustomplot.cpp都添加到工程中来。
在界面上拖拽一个widget部件,然后升级成Qcustomplot,(参考:http://www.bubuko.com/infodetail-744744.html)部件名称改为customPlot
mainwindow.h代码如下:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QTimer>
- #include "qcustomplot.h"
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- //设置qcustomplot画图属性,实时
- void setupRealtimeDataDemo(QCustomPlot *customPlot);
- private slots:
- //添加实时数据槽
- void realtimeDataSlot();
- private:
- Ui::MainWindow *ui;
- //定时器,周期调用realtimeDataSlot()槽,实现动态数据添加到曲线
- QTimer dataTimer;
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QVector>
- #include <QTimer>
- #include <QTime>
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- setupRealtimeDataDemo(ui->customPlot);
- ui->customPlot->replot(); <