在这个解决方案中,我假设表是通过以下方式创建的(使用 sqlite 作为数据库):
create table empdata (id INTEGER PRIMARY KEY AUTOINCREMENT,
time DATETIME,
temperature REAL)
我也会使用QSqlTableModel作为模型,如果你想使用QSqlQueryModel逻辑类似。
QSqlTableModel *model=new QSqlTableModel;
model->setTable("empdata");
model->select();
ui->tableView->setModel(model);
ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Temp");
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("dd/MM/yyyy hh:mm:ss");
ui->customPlot->xAxis->setTicker(dateTicker);
QVector<QCPGraphData> timeData(model->rowCount());
for(int i=0; i< model->rowCount(); ++i){
timeData[i].key = model->index(i, model->fieldIndex("time")).data().toDateTime().toTime_t();
timeData[i].value = model->index(i, model->fieldIndex("temperature")).data().toDouble();
}
double Tmin = (*std::min_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.key < y.key; })).key;
double Tmax = (*std::max_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.key < y.key; })).key;
double Ymin = (*std::min_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.value < y.value; })).value;
double Ymax = (*std::max_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.value < y.value; })).value;
ui->customPlot->xAxis->setRange(Tmin, Tmax);
ui->customPlot->yAxis->setRange(Ymin, Ymax);
ui->customPlot->graph(0)->data()->set(timeData);
ui->customPlot->replot();
下面link是完整的例子。
更新:
解决方案类似,但您必须将该 QString 转换为 QDateTime
QSqlTableModel *model=new QSqlTableModel;
model->setTable("empdata2");
model->select();
ui->tableView->setModel(model);
ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Temp");
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("hh:mm");
ui->customPlot->xAxis->setTicker(dateTicker);
QVector<QCPGraphData> timeData(model->rowCount());
for(int i=0; i< model->rowCount(); ++i){
timeData[i].key = QDateTime(QDate::currentDate(), model->index(i, model->fieldIndex("time")).data().toTime()).toTime_t();
timeData[i].value = model->index(i, model->fieldIndex("temp")).data().toDouble();
}
double Tmin = (*std::min_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.key < y.key; })).key;
double Tmax = (*std::max_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.key < y.key; })).key;
double Ymin = (*std::min_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.value < y.value; })).value;
double Ymax = (*std::max_element(timeData.begin(), timeData.end(),
[](const QCPGraphData& x, const QCPGraphData& y)
{ return x.value < y.value; })).value;
ui->customPlot->xAxis->setRange(Tmin, Tmax);
ui->customPlot->yAxis->setRange(Ymin, Ymax);
ui->customPlot->graph(0)->data()->set(timeData);
ui->customPlot->replot();
您可以在以下link找到新的解决方案