专栏名称: 面包板社区
面包板社区——中国第一电子人社交平台 面包板社区是Aspencore旗下媒体,整合了电子工程专辑、电子技术设计、国际电子商情丰富资源。社区包括论坛、博客、问答,拥有超过250万注册用户,加入面包板社区,从菜鸟变大神,打造您的电子人脉社交圈!
目录
相关文章推荐
投行小兵  ·  香港上市全流程 (从未如此详尽) ·  9 小时前  
阿尔法工场研究院  ·  那些卖不好的车跑去国外,只会毁了中国品牌的名声 ·  10 小时前  
春晖投行在线  ·  招人时确保价值观一致,然后通过企业文化来确保 ... ·  23 小时前  
心禅道  ·  投资#790 ... ·  昨天  
心禅道  ·  投资#790 ... ·  昨天  
51好读  ›  专栏  ›  面包板社区

QT 使用 customplot实现绘图

面包板社区  · 公众号  ·  · 2024-05-13 17:54

正文

从customPlot 官网下载需要的源文件。

将qcustomplot类中的源文件加入到工程里。

在工程文件中增加

greaterThan(QT_MAJOR_VERSION4): QT += widgets printsupportgreaterThan(QT_MAJOR_VERSION4): CONFIG += c++11lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11

主要是增加 printsupport支持

Ui文件中增加一个qobject,并提升为qcustomplot

定义存放点的向量

#define PLOTBUFSIZE 1000

QVector idx(PLOTBUFSIZE),vec(PLOTBUFSIZE);

Customplot 只支持double向量。

向量可以自行生成,或从文件中读入,或从通信号获得。这里是从串口获得。

初始化时连接串口到槽函数 readData()

connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);void MainWindow::readData(){QString data = serial->readAll();buf->append(data);//    QStringList datlist = data.split(",",QString::SkipEmptyParts);ui->lineEdit->setText(data);DripDataPharese(buf);// create graph and assign data to it:ui->customPlot->addGraph();ui->customPlot->graph(0)->setData(idx, vec);// give the axes some labels:ui->customPlot->xAxis->setLabel("x");ui->customPlot->yAxis->setLabel("y");// set axes ranges, so we see all data:ui->customPlot->xAxis->setRange(01000);ui->customPlot->yAxis->setRange(0,4096);ui->customPlot->replot();ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);}

这个 DripDataPharese函数负责从串口接收到的数据进行解析。找到以”DATA”开头的包,将包里的数据整合到idx 和 vec向量中。

#define PLOTBUFSIZE 1000QVector idx(PLOTBUFSIZE),vec(PLOTBUFSIZE);void DripDataPharese(QString *buf){static int16_t index = 0;int pos = buf->indexOf("DATA");if((pos != -1)&&(pos != 0))buf->remove(0,pos);QStringList datlist = buf->split("DATA",QString::SkipEmptyParts);if(datlist.count() > 1){for(int j = 0;j 1;j++){if(datlist.at(j).count() == 2*100){for(int i = 0;i<100;i++){idx[index] = index






请到「今天看啥」查看全文