QML QQuickImageProvider creation and registration

I need to create my own QImageProvider object in order to use QImage from RAM in QML. I've created it but it uses path that needs to be set from qml so it also inherits QObject and uses Q_PROPERTY definitions:

class MapImageProvider :
        public QObject,
        public QQuickImageProvider
{
        Q_OBJECT

        Q_PROPERTY(QString basePath READ basePath WRITE setBasePath NOTIFY basePathChanged)
    public:
        MapImageProvider();

    signals:
        void basePathChanged();

    public slots:
        QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) {
QFileInfo imageFile((_basePath + "/baseq3/%1.pk3").arg(id));

    if (imageFile.exists()) {
        QString extractPath((QDir::tempPath() + "/%1.jpg").arg(id));

        //use quazip to read file and return QImage
    }

    return QImage();
    }
    private:
        QString basePath() const;
        void setBasePath(const QString &) noexcept;
        static QString _basePath;
};

The problem is that I can't set it from qml plugin's side but only from QQmlEngine's side in another application (which runs qml files):

QQmlApplicationEngine engine;
engine.addImageProvider("map", MapImageProvider);
engine.load(QUrl(QStringLiteral("qrc:///ui/views/mainwindow.qml")));

return app.exec();

This is very odd because it needs headers of qml plugin in order to set my MapImageProvider and more - it will not use the path I need to be specified in order to obtain image from my image provider.

So is there any way exists to register QQuickImageProvider right from QML-C++ plugin side like qmlRegisterType<>() and others?

Answers


Found a solution:

We can do anything with QQmlEngine right from the plugin:

void initializeEngine(QQmlEngine *engine, const char *uri)
{
    Q_UNUSED(uri);
    engine->addImageProvider("map", new MapImageProvider);
}

Need Your Help

How do I bootstrap a plugin on TYPO3 CMS 6.0 with extbase?

typo3 typoscript extbase

I'm trying to use an extbase plugin through typoscript on TYPO3 CMS 6.0. I used the following code, that I found repeated all over the web:

Django - Ajax - Uploader Model: Save file

jquery python ajax django file-upload

I am trying to make a django-ajax-uploader to create a model. I have created a sample but I can't understand if there is a need to use import_uploader or create a simple class to save model with fi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.