Hi Guys,
I’ve been trying to incorporate a C++ pdf printer to my application; I have tested a simple application using Qt (not felgo) and this works, however when trying to integrate together it’s not working, I’m terrible when it comes to C++ but this is my code so far:
NOTE: I receive the error file:allbookd/qml/Main.qml:12: module “com.allbookd.pdf” is not installed
My Code is:
main.cpp
#include <QApplication>
#include <FelgoApplication>
#include <QQmlApplicationEngine>
#include <QtAndroid>
#include <QQmlContext>
#include "pdf.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FelgoApplication felgo;
felgo.setPreservePlatformFonts(true);
qmlRegisterType<pdf>("com.allbookd.pdf",3,0,"MyPdf");
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);
felgo.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));
engine.load(QUrl(felgo.mainQmlFileName()));
QtAndroid::hideSplashScreen(1000);
return app.exec();
}
pdf.h
#ifndef PDF_H
#define PDF_H
#include <QObject>
#include <QDebug>
#include <QPainter>
#include <QTextDocument>
#include <QPdfWriter>
#include <QDate>
#include <QStandardPaths>
#include <QPrinter>
#include <QFile>
class pdf : public QObject
{
Q_OBJECT
public:
explicit pdf(QObject *parent = nullptr);
void setCurrentDate();
QString getCurrentDate() const;
signals:
public slots:
QString getPdfPath() const;
void saveInvoice();
private:
QString currentDate;
QString pdfPath;
};
#endif // PDF_H
pdf.cpp
#include "pdf.h"
pdf::pdf(QObject *parent) : QObject(parent)
{
setCurrentDate();
QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
pdfPath = path + "/invoice_" + getCurrentDate() + ".pdf";
}
void pdf::setCurrentDate()
{
QDate date(QDate::currentDate());
int day = date.day();
int month = date.month();
int year = date.year();
currentDate = QString::number(day) + "_" + QString::number(month) + "_" + QString::number(year);
}
QString pdf::getCurrentDate() const
{
return currentDate;
}
QString pdf::getPdfPath() const
{
return pdfPath;
}
void pdf::saveInvoice()
{
QPdfWriter pdfWriter(getPdfPath());
pdfWriter.setPageSize(QPageSize(QPageSize::A4));
QPainter painter(&pdfWriter);
painter.scale(20.0, 20.0);
QFile file(":/html/htmlcode.html");
if(file.open(QIODevice::ReadOnly)) {
QByteArray temp = file.readAll();
QString html = temp;
QTextDocument doc;
doc.setHtml(html);
doc.drawContents(&painter);
} else {
qDebug() << "error: " << file.error();
}
}
Main.qml
import Felgo 3.0
import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0 as Quick2
import com.allbookd.pdf 3.0
import "model"
import "adminPages"
import "setupPages"
import "userPages"
import "helpPages"
import "videoPages"
import "toolkit"
//the rest isn't really needed as it wont load past the 'import' call