1、擷取目前時間
void MainWindow::dataupdate()
{
//聲明時間擷取變量,實際情況中,擷取的是目前時間的總秒數
double nowtime = QTime::currentTime().minute()*60+QTime::currentTime().second();
//設定x軸名稱
ui->complot->xAxis->setLabel("時間");
//設定x軸範圍,實際情況設定的是5秒之内的資料
ui->complot->xAxis->setRange(nowtime-5,nowtime);
//設定y軸範圍
ui->complot->yAxis->setRange(ydown,yup);
//添加資料,效果就是在曲線的右側添加資料進來,因為x資料是最新的時間
ui->complot->graph(0)->addData(nowtime,y);
//必須重新整理曲線
ui->complot->replot();
}
2、沒有擷取目前時間
(1)mainwindow.h中定義變量

(2)mainwindow.c中初始化x坐标軸
void MainWindow::QPlot_init(QCustomPlot *customPlot)
{
// 圖表添加兩條曲線
pGraph1_1 = customPlot->addGraph();
pGraph1_2 = customPlot->addGraph();
//坐标軸使用時間刻度
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
customPlot->xAxis->setTicker(timeTicker);
customPlot->xAxis->setTickLabelRotation(30);//設定x軸時間旋轉角度為30度
}
(3)x軸時間顯示效果