qcustomplot 用时间作为x坐标轴,总是为0或者8点

大年三十了,祝大家新年快乐,万事如意!

 

这个鸟东西,刚接触还不是那么容易显示时间,csdn上一大堆基本都是错误的,我这一点心得分享下。

1、首先有2种类可以设置时间标签:

QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);    timeTicker->setTimeFormat("%h:%m:%s");

ui->widCapGraphics->xAxis->setTicker(timeTicker);

 

还有种:

QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);    timeTicker->setDateTimeFormat("hh:mm:ss");

ui->widCapGraphics->xAxis->setTicker(timeTicker);

注意,这2个鬼类的时间格式是不一样的,一个带%,一个是标准的qt

2、我们要知道图表刷新的周期单位,比如毫秒、秒、天,先用秒作单位,刷新周期是1秒:

继续:http://labisart.com/blog/index.php/Home/Index/article/aid/200

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QCustomPlot是一个强大的C++图形库,可用于创建动态曲线图。如果您想要绘制X轴坐标为时间的动态曲线,可以按照以下步骤操作: 1.创建一个QCustomPlot对象并设置其X轴为时间类型: ```c++ QCustomPlot *customPlot = new QCustomPlot(); customPlot->xAxis->setType(QCPAxis::atDateTime); ``` 2.创建一个QCPGraph对象并将其添加到QCustomPlot中: ```c++ QCPGraph *graph = customPlot->addGraph(); ``` 3.在每个时间上添加数据。假设您有一个名为“data”的QVector对象,其中包含时间和值的数据: ```c++ for (int i=0; i<data.size(); ++i) { double time = QDateTime::fromString(data[i].time, "yyyy-MM-dd hh:mm:ss.zzz").toMSecsSinceEpoch(); double value = data[i].value; graph->addData(time, value); } ``` 在这里,我们将时间转换为毫秒级别,因为QCustomPlot使用毫秒作为时间单位。 4.为X轴设置时间格式。您可以使用QCustomPlot时间格式化字符串来设置您想要的日期和时间格式: ```c++ customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); ``` 5.启用自适应X轴范围,以便在动态添加数据时自动扩展X轴范围: ```c++ customPlot->xAxis->setRange(QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime().addSecs(-60)), QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime())); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(10); ``` 在这里,我们将X轴范围设置为最近60秒,然后启用手动刻度步长,并将其设置为10秒。 6.最后,您需要调用replot()函数来绘制动态曲线: ```c++ customPlot->replot(); ``` 完整代码示例: ```c++ QCustomPlot *customPlot = new QCustomPlot(); customPlot->xAxis->setType(QCPAxis::atDateTime); QCPGraph *graph = customPlot->addGraph(); for (int i=0; i<data.size(); ++i) { double time = QDateTime::fromString(data[i].time, "yyyy-MM-dd hh:mm:ss.zzz").toMSecsSinceEpoch(); double value = data[i].value; graph->addData(time, value); } customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); customPlot->xAxis->setRange(QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime().addSecs(-60)), QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime())); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(10); customPlot->replot(); ``` 希望这可以帮助您创建X轴坐标为时间的动态曲线!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值