国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

QCustomPlot實(shí)現(xiàn)實(shí)時(shí)動(dòng)態(tài)曲線

2019-11-10 18:08:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
Qt4中,可以使用QCustompPlot來(lái)繪制曲線,QCustompPlot是一個(gè)第三方工具,可以到官網(wǎng)下載:http://www.qcustomplot.com/index.php/download

這里實(shí)現(xiàn)一個(gè)實(shí)時(shí)動(dòng)態(tài)曲線圖,用隨機(jī)數(shù)作為實(shí)時(shí)數(shù)據(jù),程序運(yùn)行結(jié)果如下:

主機(jī)環(huán)境:fedora9,Qt4.7,Qtcreator 2.0.1使用Qtcreator 2.0.1新建一個(gè)工程,基類模板選擇QMainWindow。將解壓得到的QCustompPlot文件夾里面的頭文件qcustomplot.h和源文件qcustomplot.cpp復(fù)制粘貼到工程文件夾下。在Qtcreator中,對(duì)著工程名右鍵,添加已有文件,將頭文件qcustomplot.h和源文件qcustomplot.cpp都添加到工程中來(lái)。在界面上拖拽一個(gè)widget部件,然后升級(jí)成Qcustomplot,(參考:http://www.bubuko.com/infodetail-744744.html)部件名稱改為customPlotmainwindow.h代碼如下:

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QTimer>#include "qcustomplot.h"namespace Ui {    class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();    //設(shè)置qcustomplot畫圖屬性,實(shí)時(shí)    void setuPRealtimeDataDemo(QCustomPlot *customPlot);private slots:    //添加實(shí)時(shí)數(shù)據(jù)槽    void realtimeDataSlot();private:    Ui::MainWindow *ui;    //定時(shí)器,周期調(diào)用realtimeDataSlot()槽,實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)添加到曲線    QTimer dataTimer;};#endif // MAINWINDOW_Hmainwindow.cpp代碼如下:#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();    ui->checkBox_temp->setChecked(true);    ui->checkBox_hui->setChecked(true);}MainWindow::~MainWindow(){    delete ui;}//畫圖初始化void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot){//#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)  //QMessageBox::critical(this, "", "You're using Qt < 4.7, the realtime data demo needs functions that are available with Qt 4.7 to work properly");//#endif  //demoName = "Real Time Data Demo";  // include this section to fully disable antialiasing for higher performance:  /*  customPlot->setNotAntialiasedElements(QCP::aeAll);  QFont font;  font.setStyleStrategy(QFont::NoAntialias);  customPlot->xAxis->setTickLabelFont(font);  customPlot->yAxis->setTickLabelFont(font);  customPlot->legend->setFont(font);  */  customPlot->addGraph(); // blue line  customPlot->graph(0)->setPen(QPen(Qt::blue));  customPlot->graph(0)->setName("temp");  //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));  //customPlot->graph(0)->setAntialiasedFill(false);  customPlot->addGraph(); // red line  customPlot->graph(1)->setPen(QPen(Qt::red));  customPlot->graph(1)->setName("hui");  //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));  customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);  customPlot->xAxis->setDateTimeFormat("hh:mm:ss");  customPlot->xAxis->setAutoTickStep(false);  customPlot->xAxis->setTickStep(2);  customPlot->axisRect()->setupFullAxesBox();  // make left and bottom axes transfer their ranges to right and top axes:  //connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));  //connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));  // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:  connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));  dataTimer.start(0); // Interval 0 means to refresh as fast as possible  customPlot->legend->setVisible(true);}void MainWindow::realtimeDataSlot(){    //key的單位是秒    double key = QDateTime::currentDateTime().toMSeCSSinceEpoch()/1000.0;    qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);    //使用隨機(jī)數(shù)產(chǎn)生兩條曲線    double value0 = qrand() % 100;    double value1 = qrand() % 80;    if (ui->checkBox_temp->isChecked())        ui->customPlot->graph(0)->addData(key, value0);//添加數(shù)據(jù)1到曲線1    if (ui->checkBox_hui->isChecked())        ui->customPlot->graph(1)->addData(key, value1);//添加數(shù)據(jù)2到曲線2    //刪除8秒之前的數(shù)據(jù)。這里的8要和下面設(shè)置橫坐標(biāo)寬度的8配合起來(lái)    //才能起到想要的效果,可以調(diào)整這兩個(gè)值,觀察顯示的效果。    ui->customPlot->graph(0)->removeDataBefore(key-8);    ui->customPlot->graph(1)->removeDataBefore(key-8);      //自動(dòng)設(shè)定graph(1)曲線y軸的范圍,如果不設(shè)定,有可能看不到圖像//也可以用ui->customPlot->yAxis->setRange(up,low)手動(dòng)設(shè)定y軸范圍    ui->customPlot->graph(0)->rescaleValueAxis();    ui->customPlot->graph(1)->rescaleValueAxis(true);       //這里的8,是指橫坐標(biāo)時(shí)間寬度為8秒,如果想要橫坐標(biāo)顯示更多的時(shí)間    //就把8調(diào)整為比較大到值,比如要顯示60秒,那就改成60。    //這時(shí)removeDataBefore(key-8)中的8也要改成60,否則曲線顯示不完整。    ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//設(shè)定x軸的范圍    ui->customPlot->replot();}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白银市| 新津县| 桐庐县| 高陵县| 永清县| 广昌县| 娱乐| 宝兴县| 木里| 安国市| 兴义市| 高尔夫| 铁力市| 遂川县| 霞浦县| 安阳市| 沙河市| 潢川县| 上饶县| 泰安市| 莆田市| 黄骅市| 柯坪县| 迁西县| 潮安县| 枣强县| 方正县| 万盛区| 肃宁县| 曲阳县| 宁都县| 枝江市| 凤冈县| 万年县| 太仆寺旗| 武川县| 济宁市| 报价| 靖西县| 清苑县| 万荣县|