Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Using Felgo with Qt

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #20420

    Gabor

    Hi,

    In my QML app I want to use QQuickImageProvider to turn an image into QPixmap. I created the ResourceImageProvider class like this:

    resourceimageprovider.h:

    #ifndef RESOURCEIMAGEPROVIDER_H
    #define RESOURCEIMAGEPROVIDER_H
    
    #include <QQuickImageProvider>
    #include <QImage>
    #include <QPixmap>
    #include <QObject>
    
    class ResourceImageProvider : public QQuickImageProvider, public QObject
    {
        Q_OBJECT
    public:
        ResourceImageProvider();
    //    ~ ResourceImageProvider();
    
        QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
        QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
    };
    
    #endif // RESOURCEIMAGEPROVIDER_H
    

    resourceimageprovider.cpp:

    #include "resourceimageprovider.h"
    
    
    ResourceImageProvider::ResourceImageProvider() : QQuickImageProvider (QQuickImageProvider(Image))
    {
    
    
    
    }
    QImage ResourceImageProvider::requestImage (const QString &id, QSize *size, const QSize &requestedSize)
    {
        QString rsrcid = ":/" + id;
        QImage image(rsrcid);
        QImage result;
    
        if(requestedSize.isValid ()) {
            result = image.scaled (requestedSize,Qt::KeepAspectRatio);
        } else {
            result = image;
        }
    
        *size = result.size();
        return  result;
    }
    
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    {
        QString rsrcid = ":/" + id;
        QPixmap image(rsrcid);
        QPixmap result;
    
        if(requestedSize.isValid ()) {
            result = image.scaled (requestedSize,Qt::KeepAspectRatio);
        } else {
            result = image;
        }
    
        *size = result.size();
        return  result;
    }
    

    main.cpp:

    #include <QApplication>
    #include <VPApplication>
    
    #include <QQmlApplicationEngine>
    #include <QQuickImageProvider>
    #include "resourceimageprovider.h"
    
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        VPApplication vplay;
    
        // Use platform-specific fonts instead of Felgo's default font
        vplay.setPreservePlatformFonts(true);
    
        QQmlApplicationEngine engine;
      vplay.initialize(&engine);
    
        // use this during development
        // for PUBLISHING, use the entry point below
        vplay.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
    
        ResourceImageProvider *imageProvider = new ResourceImageProvider();
        imageProvider->requestPixmap("kép",140,100);
        engine.rootContext ()->setContextProperty("ImageProvider",imageProvider);
    
        // vplay.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));
    
        engine.load(QUrl(vplay.mainQmlFileName()));
    
        return app.exec();
    }
    

    How can I fix the

     imageProvider->requestPixmap("kép",140,100);

    line so I would be able to ssign values from the QML code?

    (I am trying to follow the Integrate C++ and QML example).

    Thank you for your help.

    #20422

    Kacper
    #20423

    Gabor Toth

    Hi,

    Yes, I tried. I wanted to add the c++ object as a property. My problem is that requestPixmap needs to pass 3 variables (id, size and requested size) and I couldn’t figure out how to use those variables there.

    #20427

    Günther
    Felgo Team

    To make functions or properties usable from QML, your ResourceImageProvider type requires some additional markup. For example, add them under public slots, similar to the increment(value) method of the guide:  How to Expose a Qt C++ Class with Signals and Slots to QML

    You can then call requestPixmap(…) on the ResourceImageProvider instance in your QML code.

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

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded