step:
1. 在*.pro中添加,并qmake
QT += core gui sql charts
2. 在*.ui中添加widget 并升级为QChartView
3. 程序中添加代码,绘制曲线
QSqlTableModel *model = new QSqlTableModel;
model->setTable("DATATABLE");
model->select();
if(0 == model->rowCount())
{
return;
}
QLineSeries *seriesx = new QLineSeries();
QLineSeries *seriesy = new QLineSeries();
QLineSeries *seriesz = new QLineSeries();
for(int i=0;i<model->rowCount();++i)
{
QDateTime t = model->record(i).value(2).toDateTime();
qreal dx = model->record(i).value(3).toDouble();
qreal dy = model->record(i).value(4).toDouble();
qreal dz = model->record(i).value(5).toDouble();
seriesx->append(t.toMSecsSinceEpoch(), dx);
seriesy->append(t.toMSecsSinceEpoch(), dy);
seriesz->append(t.toMSecsSinceEpoch(), dz);
}
QDateTimeAxis *axisX = new QDateTimeAxis;
axisX->setFormat("dd-MM-yyyy");
seriesx->setName("x(mm)");
seriesy->setName("y(mm)");
seriesz->setName("z(mm)");
ui->widget->chart()->addSeries(seriesx); ui->widget->chart()->addSeries(seriesy); ui->widget->chart()->addSeries(seriesz); ui->widget->chart()->removeAxis(ui->widget->chart()->axisX());ui->widget->chart()->removeAxis(ui->widget->chart()->axisY());ui->widget->chart()->setAxisX(axisX, seriesx); ui->widget->chart()->setAxisX(axisX, seriesy); ui->widget->chart()->setAxisX(axisX, seriesz); QValueAxis *axisy = new QValueAxis; qreal max = ui->comboBox->currentText().toDouble(); axisy->setRange(-max, max); ui->widget->chart()->setAxisY(axisy,seriesx); ui->widget->chart()->setAxisY(axisy,seriesy);ui->widget->chart()->setAxisY(axisy,seriesz); ui->widget->chart()->legend()->setVisible(true); ui->widget->chart()->legend()->setAlignment(Qt::AlignBottom);