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?
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);
}