This section describes how to implement a data source for KReport.
As an example, this data source will simply wrap a QAbstractTableModel and will keep the current row in an attribute. It is the most simple data source that does not sort its data.
The constructor already receives the model with all the data that we need to extract, therefore in this case there is nothing to open or close.
Next we will manage the current row by implementing the next, previous, first, and last methods. Those methods are called by KReport when traversing the data source within a report’s section.
KReport needs to know how many rows there are in the data source.
It also needs to know the field count for each record, their names, and a way to reference them. All records need to have the same number of fields, exactly like the rows of a QAbstractTableModel. In this case, the field names will correspond to column’s names, and will use them as keys; the default behaviour of KReportDataSource::fieldKeys() is to return the output of KReportDataSource::fieldNames().
KReport will need to be able to read the value of a given field. The field is always in the current record and can be referenced by the key — the column’s name in this case — or by its position. Therefore we need to handle both cases.
The last remaining method to implement is for data sources that can retrieve data from multiple sources. In this case there is one possible source, the QAbstractTableModel, and have nothing to return.
Our data source is now ready to be used to render reports.