栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

物联网(X):QCustomPlot

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

物联网(X):QCustomPlot

预先准备
  1. 添加源文件和头文件
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport添加打印支持
  3. 在界面新建widget并且提升为qcustomplot
基础绘图
void MainWindow::create(QCustomPlot* customPlot){
    QVector x(101),y(101);
    for(int i=0;i<101;i++){
        x[i]=i/50.0-1;
        y[i]=x[i]*x[i];
    }
    customPlot->addGraph();
    customPlot->graph(0)->setData(x,y);
    customPlot->yAxis->setLabel("x");
    customPlot->xAxis->setLabel("y");
    customPlot->yAxis->setRange(0,1);
    customPlot->xAxis->setRange(-1,1);
}
动态时间绘图
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)//实时显示
{

  customPlot->addGraph(); //在面板添加曲线
  customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));//曲线颜色

  QSharedPointer timeTicker(new QCPAxisTickerTime);//智能指针
  timeTicker->setTimeFormat("%h:%m:%s");//对指针设置时间格式
  customPlot->xAxis->setTicker(timeTicker);//x的坐标区间
  customPlot->axisRect()->setupFullAxesBox();//坐标轴撑满面板
  customPlot->yAxis->setRange(-1.2, 1.2);//y坐标范围

  connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
  connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

  connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));//当定时器溢出之后
  dataTimer.start(0); //开启定时器
}


void MainWindow::realtimeDataSlot()
{
  static QTime timeStart = QTime::currentTime();//从当前时间开始
  double key = timeStart.msecsTo(QTime::currentTime())/1000.0;
  static double lastPointKey = 0;
  if (key-lastPointKey > 0.002)//每2毫秒添加一个点
  {
    ui->widget->graph(0)->addData(key,qSin(key/0.3843));//添加一个点
    lastPointKey = key;
  }
  ui->widget->xAxis->setRange(key, 10, Qt::AlignRight);//位置,大小,对齐
  ui->widget->replot();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号