Connecting QQuickImageProvider
I have to connect a QQuickImageProvider with a class to pass an image that the image provider must return, but I am not finding a way to do that.
I have a class called provedorImagem.cpp with a virtual requestImage function implemented and I have also a class called processaImagem.cpp that is the class to perform modification at the image.
The provedorImagem class is passed to engine as a provider: engine.addImageProvider("provedor", provedorImg) in main.cpp
What I need is a way to connect a slot in the provider in main.cpp with a signal in processaImagem.cpp. Doing that the processaImagem.cpp can send the image I must return to Qml to provedorImagem.cpp and send it back to Qml.
Could someone help me?
Code below
main.cpp
Code:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "processaimagem.h"
#include "provedorimagem.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<processaImagem>("ProcessaImagemQml", 1, 0, "ProcessaImagem");
QQmlApplicationEngine engine;
provedorImagem *provedorImg = new provedorImagem;
//------------ I have to create a connection here between the provider slot and a signal in processaImagem with the image to provide -----------------
engine.addImageProvider("provedor", provedorImg);
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
processaImagem.h
Code:
#ifndef PROCESSAIMAGEM_H
#define PROCESSAIMAGEM_H
#include <QObject>
#include <QImage>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickImageProvider>
#include "provedorimagem.h"
class processaImagem
: public QObject{
Q_OBJECT
public slots:
public:
processaImagem
(QObject *parent
= 0);
signals:
};
#endif // PROCESSAIMAGEM_H
processaImagem.cpp
Code:
#include "processaimagem.h"
#include <QDebug>
processaImagem
::processaImagem(QObject *parent
){
}
{
QImage imagem
= this
->carregaImagem
(caminho
);
if(imagem.isNull())
{
qDebug() << "Erro ao receber a imagem";
}
else
{
qDebug() << "Imagem recebida";
caminhoRetorno = "image://provedor/imagemEditada";
}
return caminhoRetorno;
}
{
QUrl caminhoImagem
(caminho
);
QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
QString imageId
= caminhoImagem.
path().
remove(0,
1);
QImage imagem
= imageProvider
->requestImage
(imageId,
&imageSize, imageSize
);
if(imagem.isNull())
{
qDebug() << "Erro ao carregar a imagem";
}
else
{
qDebug() << "Imagem carregada";
}
return imagem;
}
provedorimagem.h
Code:
#ifndef PROVEDORIMAGEM_H
#define PROVEDORIMAGEM_H
#include <QImage>
#include <QQuickImageProvider>
class provedorImagem : public QQuickImageProvider
{
public:
provedorImagem();
void carregaImagem();
public slots:
void carregaImagem
(QImage imagemRecebida
);
private:
};
#endif // PROVEDORIMAGEM_H
provedorimagem.cpp
Code:
#include "provedorimagem.h"
#include <QDebug>
provedorImagem::provedorImagem() : QQuickImageProvider(QQuickImageProvider::Image)
{
}
{
if(imagem.isNull())
{
qDebug() << "Erro ao prover a imagem";
}
else
{
qDebug() << "Imagem provida";
}
return imagem;
}
void provedorImagem
::carregaImagem(QImage imagemRecebida
) {
imagem = imagemRecebida;
}
Re: Connecting QQuickImageProvider
You set the provider object also as a context property on the engine's root context.
Then you handle the signal of the QML object in QML and call the slots from there.
Cheers,
_
Re: Connecting QQuickImageProvider
Could you post here a little piece o code to exemplify? I am not figuring out how can I do that.
I tried to do this way, but it didnt work.
Code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<processaImagem>("ProcessaImagemQml", 1, 0, "ProcessaImagem");
QQmlApplicationEngine engine;
provedorImagem *provedorImg = new provedorImagem;
//provedorImagem provedorImg;
//engine.rootContext()->setContextProperty("ProvedorImagem", provedorImg);
QObject *processadorImagem
= engine.
findChild<QObject
*>
("processadorImagem");
QObject::connect(processadorImagem,
SIGNAL(enviaImagem
(QImage)), provedorImg,
SLOT(carregaImagem
(QImage)));
//------------A CHAVE É AQUI - CRIAR UMA CONEXÃO
engine.addImageProvider("provedor", provedorImg);
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
Re: Connecting QQuickImageProvider
Commented out code is not compiled into the executable, so it is never executed.
Also the code for the QML is missing, thus it is impossible to tell how you use the context property object.
Alternatively you could get the image provider from the engine inside the code of the other class and connect there.
Cheers,
_