我是Qt创建者C ++的新手。我已经设计了一个应用程序,其中数据显示在从Sqlite数据库中获取的tableView上,直到在这里我都成功了,但是现在我想通过提供数据库中x和y轴的值在自定义绘图上绘制图形取得它并显示在tableView小部件上。示例:数据库中的数据就像时间和温度,现在我想在时间中提供X轴的值,在温度中提供Y轴的值,请在代码中帮我一下-
我能够按照下面的代码所示正常绘制图形,如何在x和y轴上添加数据库值。
void MainWindow::makePlot()
{
QVector<double> x(100), y(101);
// x[0]=1;Here I want the application to take the values from tableView,like
Time on X axis and Temp on Y axis.
// x[1]=2;
// x[2]=3;
// x[3]=4;
// y[0]=1;
// y[1]=2;
// y[2]=3;
// y[3]=4;
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);
ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Temp");
ui->customPlot->xAxis->setRange(1, 15);
ui->customPlot->yAxis->setRange(1, 15);
ui->customPlot->replot();
}
Here is the code that displays the Database in tableView on push button.
void MainWindow::on_pushButton_clicked()
{
MainWindow conn;
QSqlQueryModel * modal=new QSqlQueryModel();
conn.connOpen();
QSqlQuery* qry=new QSqlQuery(conn.mydb);
qry->prepare("select * from empdata");
qry->exec();
modal->setQuery(*qry);
ui->tableView->setModel(modal);
conn.connClose();
qDebug() <<(modal->rowCount());
}[![enter image description here][1]][1]
请帮助我。。谢谢。。
答案 0 :(得分:0)
在此解决方案中,我假定该表是以以下方式创建的(使用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
中找到新的解决方案