Forums

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

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

    Felgo User

    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.

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