我是Qt创作者C的新手 . 我设计了一个应用程序,其中数据显示在一个tableView从Sqlite数据库中取出,直到这里我成功了,但现在我想通过给出数据库中x和y轴的值来绘制图表上的图表 . 获取它并显示在tableView小部件上 . 示例:我的数据库中的数据就像时间和温度 - 现在我想在时间和Y轴上给出X轴的温度值,请在代码中帮助我 - 我能够正常绘制图形,如下面的代码所示,我如何在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]
请帮帮我..谢谢你...
1 回答
在此解决方案中,我假设表以下列方式创建(使用sqlite作为数据库):
我还将使用QSqlTableModel作为模型,如果要使用QSqlQueryModel,则逻辑类似 .
在下面的link是完整的例子 .
Update:
解决方案类似,但您必须将该QString转换为QDateTime
您可以在以下link中找到新的解决方案