【问题标题】:qt c++ plot customplot from sqlite tableqt c++ 从 sqlite 表中绘制 customplot
【发布时间】:2018-12-23 16:34:19
【问题描述】:

我是 Qt creator C++ 的新手。我设计了一个应用程序,其中数据显示在 tableView 上,从 Sqlite 数据库中获取数据,直到这里我成功了,但现在我想通过从我拥有的数据库中给出 x 和 y 轴的值来在 customplot 上绘制图形获取它并显示在 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]

请帮帮我..提前谢谢..

enter image description here

【问题讨论】:

  • @eyllanesc..感谢您的关注...我在 sqlite 浏览器数据库中创建了这个,我将时间变量指定为 NUMERIC,并且我手动将数据指定为 10:20 和 10: 25 等等,所以我认为时间格式应该不是问题吧?

标签: c++ qt sqlite qt5 qcustomplot


【解决方案1】:

在这个解决方案中,我假设表是通过以下方式创建的(使用 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找到新的解决方案

【讨论】:

  • 非常感谢您提供的代码,我浏览了给定链接中的代码...虽然看起来很复杂 :)...但是我需要传递表格中的数据没有 ant datetime 格式,因为我在 sqlite db 中手动将 Time 值设置为 10:20 和 10:25 等等,所以我只想知道如何将这两个变量值、Time 和 Temp 值传递给 X 和 Y直接轴...再次感谢您的时间和帮助老兄...
  • 我附上了我的 db 的截图,因为我无法与您分享 .db 文件...请完成需要..再次感谢
  • 这些字段就像..for Time: NUMERIC and for Temp: INTEGER
猜你喜欢
  • 1970-01-01
  • 2011-04-07
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2017-08-16
  • 2020-07-27
相关资源
最近更新 更多