本文主要是介绍QT/C++标签文档报表工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QT/C++标签文档报表工具
- 一、演示预览
- 二、使用步骤
- 1.环境配置
- 二、核心代码
- 1.报表程序
- 2. 数据库程序
- 三、代码链接
一、演示预览
二、使用步骤
1.环境配置
1、第一次在电脑上需要注册报表库,要不让软件运行会报错,这里已经写好了批处理直接运行就行。
2、批处理的文件主要是注册dll和安装sqlite驱动,具体如下
3、ReportUser模块第一次编译的时候会报错,不用去理他,编译第二次就可以
二、核心代码
1.报表程序
void ReportUser::Init()
{// 第一次打开先注册QString strPath = QCoreApplication::applicationDirPath() + "/FirstOpen.rpt";QDir dir;if (!dir.exists(strPath)){QString strBatDir = QCoreApplication::applicationDirPath() + "/Data";QString strBatFile = QCoreApplication::applicationDirPath() + "/Data/install.bat";QProcess p(NULL);p.setWorkingDirectory(strBatDir);p.start(strBatFile);p.waitForFinished();dir.mkpath(strPath);}// 加载数据库SqlUser::GetInstance()->InitDB();LoadSourcesData(g_qlite, g_tableName);// 加载模板QString strPathFr3 = QCoreApplication::applicationDirPath() + "/Data/data.fr3";LoadTemplateFile(strPathFr3);
}bool ReportUser::LoadSourcesData(const char* chDataSource, const char* chTableName)
{CoInitialize(NULL);bool bRet = true;try{m_pData->pReport = IfrxReportPtr(__uuidof(TfrxReport));m_pData->pDB = IfrxADODatabasePtr(__uuidof(TfrxADODatabase));m_pData->pDT = IfrxADOTablePtr(__uuidof(TfrxADOTable));m_pData->pQR = IfrxADOQueryPtr(__uuidof(TfrxADOQuery));// Set the database object propertiesm_pData->pDB->ConnectionString = chDataSource;m_pData->pDB->LoginPrompt = false;m_pData->pDB->Connected = true;// Set the table object propertiesconst char* chTableNameTemp = chTableName;m_pData->pDT->PutDatabase(m_pData->pDB);m_pData->pDT->Name = chTableNameTemp;m_pData->pDT->TableName = chTableNameTemp;// Set the query object propertiesm_pData->pQR->PutDatabase(m_pData->pDB);char buf[256];const char *chTemp = "select * from ";memset(buf, 0, sizeof(buf));strcpy(buf, chTemp);strcat(buf, chTableNameTemp);m_pData->pQR->Query = buf;// Link ADO table and ADO query to the Report objectm_pData->pReport->SelectDataset(true, IfrxDataSetPtr(m_pData->pDT));m_pData->pReport->SelectDataset(true, IfrxDataSetPtr(m_pData->pQR));}catch (_com_error e){bRet = false;CoUninitialize();return bRet;}CoUninitialize();return bRet;
}
2. 数据库程序
代码如下:
// 打开数据库
bool SqlUser::InitDB()
{if (QSqlDatabase::contains("qt_sql_default_connection")){m_dataBase = QSqlDatabase::database("qt_sql_default_connection");return true;}else{// 建立和SQlite数据库的连接m_dataBase = QSqlDatabase::addDatabase("QSQLITE");// 设置数据库文件的名字m_dataBase.setDatabaseName("ReportDatabase.db");}if (!m_dataBase.open()){m_bConnected = false;qDebug() << "Error: Failed to connect database." << m_dataBase.lastError();}elsem_bConnected = true;return m_bConnected;
}// 执行sql语句
bool SqlUser::Excute(QString sql)
{if (!m_bConnected) return false;QSqlQuery query;query.prepare(sql);bool success=query.exec(sql);if(!success){qDebug() << "Error:" << query.lastError();return false;}return true;}// 创建表
bool SqlUser::CreatTable()
{bool bRet = false;if (m_strTableNameList.size() <= 0)return bRet;QString strCreateTable = QString(u8"CREATE TABLE ReportData(SysIndex INTEGER PRIMARY KEY");for (int i = 0; i < m_strTableNameList.size(); i++){if (i < (m_strTableNameList.size() - 1))strCreateTable = strCreateTable + "," + m_strTableNameList[i] + " VARCHAR(256)";elsestrCreateTable = strCreateTable + "," + m_strTableNameList[i] + " VARCHAR(256)" + ")";}bRet = Excute(strCreateTable);return bRet;
}
三、代码链接
代码链接
https://download.csdn.net/download/u013083044/85120988
这篇关于QT/C++标签文档报表工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!