首页 文章

qt c从sqlite表中绘制customplot

提问于
浏览
0

enter image description here
我是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]

output of the above code

请帮帮我..谢谢你...

enter image description here

1 回答

  • 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();
    

    enter image description here

    在下面的link是完整的例子 .


    Update:

    解决方案类似,但您必须将该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();
    

    enter image description here

    您可以在以下link中找到新的解决方案

相关问题