Coder Social home page Coder Social logo

limereport_demonstration's Introduction

LimeReport_Demonstration

这是我自己用于学习和复现报表生成软件项目LimeReport的一个示例项目,本项目会将原LimeReport项目QMake构建系统替换成CMake构建系统,并使用LimeReport编译的库,调用库的函数现实各类函数的调用和调试。

原项目导航:https://github.com/fralx/LimeReport

0 前言

先简单介绍一下这个项目,这个项目叫做LimeReport,是一位俄罗斯小哥开发并开源的一个基于Qt和C++的报表生成软件。也就是基于提供的数据自动生成表格文档,相当于一个自动化的文档整理工具。很多的设备厂商会配备一个报表生成软件,用来生成设备数据报表,便于查看和分析。

1 环境配置

1.1 编译项目,获取库文件

编译项目过程很简单,直接下载源码,用QtCreator打开,直接编译即可在想要的Demo目录中得到相应的库函数。就像这样:

使用QtCreator编译LimeReport项目得到的库函数的位置截图

1.2 项目配置

如果是QMake的话,那么需要配置以下几点,主要包括:

  • 将库文件lib复制到自己的项目中
  • pro文件中添加相应的配置,具体如下:
QT += printsupport qml 

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/librelease/ -llimereport
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llimereportd
 
INCLUDEPATH += $$PWD/librelease
DEPENDPATH += $$PWD/librelease
 
INCLUDEPATH += $$PWD/lib
DEPENDPATH += $$PWD/lib
  • 在项目中引入库并测试
#include "lib/include/lrreportengine.h"

LimeReport::ReportEngine *report;
report = new LimeReport::ReportEngine(this);  

report->designReport();
report->previewReport();

C++this指针是一个指向当前对象的特殊指针,在类的成员函数中使用这个指针可以访问当前对象的成员函数和成员变量。类的静态成员函数是没法使用this指针的,原因如下:

  • 类的静态成员函数内部没有this指针。
  • 静态成员函数是类的全局函数,属于整个类,只与类的静态成员变量及其它类的成员函数交互,所以从设计的角度来说它就没必要有this指针。

如何更改为CMake项目?

  • CMakeLists.txt文件中添加如下内容:
# Find the required Qt5 components
find_package(Qt5 COMPONENTS Core Gui PrintSupport Qml REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

# Add Qt5 modules to target link libraries
target_link_libraries(YOU_PROJECT_NAME PRIVATE
    Qt5::Core
    Qt5::Gui
    Qt5::PrintSupport
    Qt5::Qml
)

if(WIN32)
    target_link_libraries(MIL_ReportView PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/limereportd.lib)
endif()
  • 在主函数中添加操作函数文件limereport.cpplimereport.h并配置相关操作类LeReport
#ifndef LIMEREPORT_H
#define LIMEREPORT_H

#include <QWidget>
#include "lib/include/lrreportengine.h"

class LmReport : QObject
{
    Q_OBJECT
public:
    LmReport(QWidget* parent = nullptr);
    ~LmReport();

    void LoadDesignReport();

private:
    LimeReport::ReportEngine *m_report;


};

#endif // LIMEREPORT_H
#include "limereport.h"

LmReport::LmReport(QWidget* parent)
{
    m_report = new LimeReport::ReportEngine(this);
}

LmReport::~LmReport()
{

}

void LmReport::LoadDesignReport()
{
    m_report->designReport();
}

主函数

#include <QApplication>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "limereport.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    //QGuiApplication app(argc, argv);
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    //engine.load(url);

    LmReport lreport;
    lreport.LoadDesignReport();

    return app.exec();
}
  • 【注意】在主函数中将QGuiApplication更改为QApplication,否则报错!!!
QGuiApplication app(argc, argv); --> QApplication app(argc, argv);

2 功能测试

在项目中新增两个文件,一个源文件limereport.cpp和头文件limereport.h,文件内容如下:

#ifndef LIMEREPORT_H
#define LIMEREPORT_H

#include <QDebug>
#include <QWidget>
#include <QFileInfo>
#include <QGestureEvent>
#include <QPinchGesture>
#include "lib/include/lrreportengine.h"
#include "lib/include/lrcallbackdatasourceintf.h"

class limereport : public QObject
{
    Q_OBJECT

public:
    limereport(QWidget* parent = nullptr); // 修改默认参数为nullptr
    virtual ~limereport();

public slots:
    void designReport();        //设计报表
    void previewReport();       //预览报表

private:
    LimeReport::ReportEngine *m_report;
    LimeReport::PreviewReportWidget *m_preview;
};

#endif

头文件内容如下:

#include "limereport.h"

limereport::limereport(QWidget* parent) // 使用初始化列表初始化 m_report
{
    m_report = new LimeReport::ReportEngine(this);
    m_preview = m_report->createPreviewWidget();

    // 配置模板文件数据
    QFileInfo fileInfo("E:/Other/Workspace/3DPrint/MetalForgeX/MILReportView/MIL_ReportViewer_Qt5/MIL_ReportView/ReportViewCtrl/Report/lrxml/WorkData.lrxml");
    if(fileInfo.isFile())
    {
        m_report->loadFromFile("E:/Other/Workspace/3DPrint/MetalForgeX/MILReportView/MIL_ReportViewer_Qt5/MIL_ReportView/ReportViewCtrl/Report/lrxml/WorkData.lrxml");
        m_preview->refreshPages();
    }
    m_preview->setScalePercent(50);
}

limereport::~limereport()
{
    delete m_report;
}

void limereport::designReport()
{
    m_report->designReport();
}

void limereport::previewReport()
{
    m_preview->show();
}

在主函数中调用

limereport lreport;
lreport.designReport();

如果没有文件,将会看到以下界面:

Limereport设计报表界面

3 重要功能实现

3.1 报表设计功能

3.1.1 报表组成

关于报表设计主要包含以下内容:

  • Toolbars 工具栏
  • Report Elements Bar 报表元素栏
  • Object Browser 对象浏览
  • Data Browser 数据浏览
  • Report Page 报表页面
  • Report Structure Browser 报表结构浏览

报表设计中常用的快捷键:

序号 快捷键 描述
1 Ctrl + N 新建报表
2 Ctrl + O 加载报表
3 Ctrl + S 保存报表
4 Ctrl + Shift + S 另存为
5 Ctrl + P 预览报告
6 Ctrl + Z 回退
7 Ctrl + Shift + Z 恢复
8 Ctrl + C 复制
9 Ctrl + X 剪切
10 Ctrl + V 粘贴
11 Ctrl + Arrows 移动选择的对象
12 Shift + Arrows 修改选择对象的大小
13 Del 删除选择的对象
14 Shift + Left mouse button 创建选择区域

3.1.2 数据源

LimeReport提供这么几种数据源

  • 变量,
  • 使用数据库连接的基于 SQL 的数据集。可以通过以下方式建立数据库连接:
    • 可以立即从报告生成器建立连接。
    • 可以通过外部应用程序建立连接。
  • 程序中创建一个类并让它继承自 QAbstractItemModel,然后实现这个类的各种方法和功能,从而使外部的数据集可以在 Qt 应用程序中使用。
  • 使用信号-槽(Signal-Slot)方法进行数据传输和与报告生成器的链接。

Sqlite

报表预览功能

报表自动生成

设置背景颜色

3.1 文件预览

模板文件:.lrxml

数据文件:.lrd

待定

待定

待定

3.2 模板设计

3.3 数据加载

Sqlite读取数据

循环读取数据

模板设计

模板设计的使用技巧

关于License

参考资料

  1. https://blog.csdn.net/moudide/article/details/122936070

limereport_demonstration's People

Contributors

alansortorz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.