Hello all,
I would like to dynamically load data from the mysql database to the report, the following code correct?
void CShowLimeReport::PreviewReport()
{
LimeReport::ReportEngine *report = new LimeReport::ReportEngine(m_pWnd);
QSqlDatabase db = QSqlDatabase::addDatabase(m_strDbType);
db.setHostName(m_strHostName);
db.setDatabaseName(m_strDbName);
db.setUserName(m_strUserName);
db.setPassword(m_strPassword);
if (db.open())
{
m_customers = new QSqlQuery(m_strQuerySql, db);
LimeReport::ICallbackDatasource * callbackDatasource = report->dataManager()->createCallbackDatasouce(m_strDatasourceName);
QObject::connect(callbackDatasource, SIGNAL(getCallbackData(LimeReport::CallbackInfo, QVariant&)), m_pWnd, SLOT(slotGetCallbackData(LimeReport::CallbackInfo info, QVariant &data)));
QObject::connect(callbackDatasource, SIGNAL(changePos(const LimeReport::CallbackInfo::ChangePosType&, bool&)), m_pWnd, SLOT(slotChangePos(const LimeReport::CallbackInfo::ChangePosType &type, bool &result)));
report->loadFromFile(m_strReprotName);
report->previewReport();
};
}
void CShowLimeReport::slotGetCallbackData(LimeReport::CallbackInfo info, QVariant &data)
{
if (!m_customers) return;
prepareData(m_customers, info, data);
}
void CShowLimeReport::prepareData(QSqlQuery* ds, LimeReport::CallbackInfo info, QVariant &data)
{
switch (info.dataType) {
case LimeReport::CallbackInfo::ColumnCount:
data = ds->record().count();
break;
case LimeReport::CallbackInfo::IsEmpty:
data = !ds->first();
break;
case LimeReport::CallbackInfo::HasNext:
data = ds->next();
ds->previous();
break;
case LimeReport::CallbackInfo::ColumnHeaderData:
if (info.index < ds->record().count())
data = ds->record().fieldName(info.index);
break;
case LimeReport::CallbackInfo::ColumnData:
data = ds->value(ds->record().indexOf(info.columnName));
break;
default: break;
}
}
void CShowLimeReport::slotChangePos(const LimeReport::CallbackInfo::ChangePosType &type, bool &result)
{
QSqlQuery* ds = m_customers;
if (!ds) return;
if (type == LimeReport::CallbackInfo::First) result = ds->first();
else result = ds->next();
}