替换 QTime::elapsed()

Replace QTime::elapsed()

我想绘制 sin(t) 其中 t 是以秒为单位的时间:

void MainWindow::realtimePlot()
{

    static QTime time(QTime::currentTime());
    double key = time.elapsed()/1000.0;
    QTextStream(stdout)<<(key);
    static double lastPointKey = 0;
    if(key - lastPointKey > 0.002)
    {
        ui->widget->graph(0)->addData(key, sin(key));

        lastPointKey = key;
    }

    ui->widget->graph(0)->rescaleValueAxis();
    ui->widget->xAxis->setRange(key, 4, Qt::AlignRight);
    ui->widget->replot();
}

这是我的文档代码变体:https://www.qcustomplot.com/index.php/demos/realtimedatademo 但是 elapsed 已经过时了,我应该用什么来代替?

使用QElapsedTimer:

static QElapsedTimer timer;
double key = timer.elapsed() / 1000.0;