This is my humble beginnings in porting LimeReport over to QML but it doesn't work until after a few tweaks which are the following:
- Obviously, you have to build LimeReport and add it to your project.
- You have to add the following in the main '.pro' file:
QT += core gui sql printsupport xml
these are the dependencies of LimeReport and they're required for your Qt Quick application to even compile 3. Replace the QGuiApplication with QApplication. Every Qt application runs a single QCoreApplication object which is instantiated in the 'main.cpp' file but in QML application the child QGuiApplication class is used instead of QApplication because it is made for QtWidgets applications so we have to put QApplication instead of QGuiApplication because, while it's true our app is made only using Qt Quick/QML, LimeReport is still dependent on QtWidgets so we have to make that change as follows:
int main(int argc, char * argv[]){
...
QApplication app(argc, argv);
...
}
You can now use the 'setContextProperty()' function to expose the C++ wrapper for use in QML and use it as follows:
Main.cpp
QmlReportEninge *engine = new QmlReportEngine();
...
engine.setContextProperty("ReportEngine", engine);
myQml.qml
Button {
id: print
onClicked: {
//This will set the query for the QSqlQueryModel inside ReportEngine
ReportEngine.setReportQuery("SELECT * FROM Payroll)
//You can add external variables here
ReportEngine.addVariable("monthFilter", Date().getMonth)
//Load the report template from file or use reportFromString(str) to load a template from a string
ReportEngine.reportFromFile("/path/to/template.lrxml")
//Show a preview of the report
ReportEngine.previewReport()
}
}