Forums

OverviewFelgo 2 Support (Qt 5) › Exposing C++ Class with Signals/Slots

Tagged: , , ,

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #23541

    Edward

    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

     

     

     

     

    #23542

    jrrobles79

    Have you cleaned and rebuilded your project? Every time I got that error is because the application haven’t compiled all the new c++ classes I have added, also I will add a signal somewhere to tell the qml that the invoice has been saved

     

    //on h
    signals:
        void invoiceSaved(int error);
    
    
    //on cpp
    void pdf::saveInvoice()
    {
    int error = 0 // 0 all ok , 1 error    
    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();
    error = 1;
        }
    
    emit invoiceSaved(error)       
    
    }
    #23543

    Edward

    Thanks for the tip on signalling the completion!

    I’ve cleaned/qmake/rebuilt my project and nothing seems to be working sadly….. unless it’s something in my .pro file?

     

    CONFIG += felgo # old: CONFIG += vplay
    
    PRODUCT_IDENTIFIER = com.XXX.XXXX.XXX
    PRODUCT_VERSION_CODE = 33
    
    qmlFolder.source = qml
    
    assetsFolder.source = assets
    DEPLOYMENTFOLDERS += assetsFolder
    
    RESOURCES += resources.qrc # uncomment for publishing
    
    QML_IMPORT_PATH =
    
    QML_DESIGNER_IMPORT_PATH =
    
    
    QT += androidextras \
    quick gui core printsupport
    
    
    SOURCES += \
        main.cpp \
        pdf.cpp
    
    HEADERS += \
        pdf.h
    
    
    android {
        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
        OTHER_FILES += android/AndroidManifest.xml       android/build.gradle
    }
    
    ios {
        QMAKE_INFO_PLIST = ios/Project-Info.plist
        OTHER_FILES += $$QMAKE_INFO_PLIST
    
        FELGO_PLUGINS += firebase
        FELGO_PLUGINS += onesignal
    }
    
    DISTFILES += \
        //files normally here
    
    
    

     

    #23544

    jrrobles79

    Maybe, but I don’t see where can be the error, I had a similar issue a few days ago when I added the felgo licence key on the pro file:

     

    # contains(ANDROID_TARGET_ARCH, arm64-v8a) {
    #   PRODUCT_VERSION_CODE=1011 # add 1 as suffix
    #   PRODUCT_LICENSE_KEY = "FGH..."
    # } else {
    # PRODUCT_VERSION_CODE=1010 # add 0 as suffix
    # PRODUCT_LICENSE_KEY="ABC...."
    # }
    

    I had to comment all and rebuild the project to make the new class work.

    #23547

    Edward

    Okay,

    It seems to be working –

    cleaning/qmake/building was unsuccessful, but I deleted the directories, restarted my pc, clean/qmake/build and it worked!

     

     

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support