Good afternoon,
I am facing a similar situation.
A database of power consumption readings.
Basically it consists of two tables (one with different meters and one with readings).
I have just installed qwt 6.1rc2 because it seems to have some useful features for this.
Actually the code (not elegant but ok for a beginner, I hope) works and gives me a graph.
Unfortunately the x-axis is totally cryptic. I am still struggling with QwtDateScaleEngine and QwtDateScaleDraw.
However, maybe the first part helps and maybe somebody gives me a hint about the usage of the scale engine and scale draw:
model->setTable("tbl_zaehlerstaende"); // table with meter readings
model
->setRelation
(2,
QSqlRelation("tbl_zaehler",
"zaehler_ID",
"name"));
//QSqlRelationalTableModel to link the two tables model->setFilter("name = 'my_meter'"); //show only my own consumption
model->sort(1, Qt::AscendingOrder); //sort in chronological order
model->select();
ui->tv_overview->setModel(model);
ui->tv_overview->show();
int k = model->rowCount(); //number of datapoints, ie readings
QVector<QPointF> datapoints;
for (int i = 0; i < k; i++)
{
QDateTime date;
//the format in MySql is actually just DATE, but it does not seem to matter date = (model->data(model->index(i,1))).toDateTime();
qreal x;
x = QwtDate::toDouble(date); //qwt needs a double to work
datapoints.
append(QPointF(x, qreal
(model
->data
(model
->index
(i,
3)).
toInt())));
}
curve->setSamples(datapoints);
curve->attach(ui->qwt_plot);
ui->qwt_plot->replot();
model = new QSqlRelationalTableModel;
model->setTable("tbl_zaehlerstaende"); // table with meter readings
model->setRelation(2, QSqlRelation("tbl_zaehler", "zaehler_ID", "name")); //QSqlRelationalTableModel to link the two tables
model->setFilter("name = 'my_meter'"); //show only my own consumption
model->sort(1, Qt::AscendingOrder); //sort in chronological order
model->select();
ui->tv_overview->setModel(model);
ui->tv_overview->setItemDelegate(new QSqlRelationalDelegate(ui->tv_overview));
ui->tv_overview->show();
int k = model->rowCount(); //number of datapoints, ie readings
QVector<QPointF> datapoints;
for (int i = 0; i < k; i++)
{
QDateTime date; //the format in MySql is actually just DATE, but it does not seem to matter
date = (model->data(model->index(i,1))).toDateTime();
qreal x;
x = QwtDate::toDouble(date); //qwt needs a double to work
datapoints.append(QPointF(x, qreal(model->data(model->index(i,3)).toInt())));
}
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setSamples(datapoints);
curve->attach(ui->qwt_plot);
ui->qwt_plot->replot();
To copy to clipboard, switch view to plain text mode
Really I used google, I read the documentation. But I find it hard to get started.
Many greetings,
RogerWilco
Bookmarks