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
Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3.  
  4. #include <QtQml>
  5.  
  6. #include "processaimagem.h"
  7. #include "provedorimagem.h"
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QGuiApplication app(argc, argv);
  12.  
  13. qmlRegisterType<processaImagem>("ProcessaImagemQml", 1, 0, "ProcessaImagem");
  14.  
  15. QQmlApplicationEngine engine;
  16.  
  17. provedorImagem *provedorImg = new provedorImagem;
  18.  
  19. //------------ I have to create a connection here between the provider slot and a signal in processaImagem with the image to provide -----------------
  20.  
  21. engine.addImageProvider("provedor", provedorImg);
  22.  
  23. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  24.  
  25. return app.exec();
  26. }
To copy to clipboard, switch view to plain text mode 

processaImagem.h
Qt Code:
  1. #ifndef PROCESSAIMAGEM_H
  2. #define PROCESSAIMAGEM_H
  3.  
  4. #include <QObject>
  5. #include <QImage>
  6. #include <QQmlEngine>
  7. #include <QQmlContext>
  8. #include <QQuickImageProvider>
  9.  
  10. #include "provedorimagem.h"
  11.  
  12. class processaImagem : public QObject
  13. {
  14. Q_OBJECT
  15.  
  16. public slots:
  17. QString recebeImagem(const QString &caminho);
  18.  
  19. public:
  20. processaImagem(QObject *parent = 0);
  21.  
  22. QImage carregaImagem(const QString &caminho);
  23.  
  24. signals:
  25. void enviaImagem(QImage);
  26. };
  27.  
  28. #endif // PROCESSAIMAGEM_H
To copy to clipboard, switch view to plain text mode 

processaImagem.cpp
Qt Code:
  1. #include "processaimagem.h"
  2.  
  3. #include <QDebug>
  4.  
  5. processaImagem::processaImagem(QObject *parent)
  6. {
  7.  
  8. }
  9.  
  10. QString processaImagem::recebeImagem(const QString &caminho)
  11. {
  12. QImage imagem = this->carregaImagem(caminho);
  13.  
  14. QString caminhoRetorno;
  15.  
  16. if(imagem.isNull())
  17. {
  18. qDebug() << "Erro ao receber a imagem";
  19. }
  20. else
  21. {
  22. qDebug() << "Imagem recebida";
  23. caminhoRetorno = "image://provedor/imagemEditada";
  24. }
  25.  
  26. return caminhoRetorno;
  27. }
  28.  
  29. QImage processaImagem::carregaImagem(const QString &caminho)
  30. {
  31. QUrl caminhoImagem(caminho);
  32. QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
  33. QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
  34. QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
  35.  
  36. QSize imageSize;
  37. QString imageId = caminhoImagem.path().remove(0, 1);
  38. QImage imagem = imageProvider->requestImage(imageId, &imageSize, imageSize);
  39.  
  40. if(imagem.isNull())
  41. {
  42. qDebug() << "Erro ao carregar a imagem";
  43. imagem = QImage();
  44. }
  45. else
  46. {
  47. qDebug() << "Imagem carregada";
  48. }
  49.  
  50. return imagem;
  51. }
To copy to clipboard, switch view to plain text mode 

provedorimagem.h
Qt Code:
  1. #ifndef PROVEDORIMAGEM_H
  2. #define PROVEDORIMAGEM_H
  3.  
  4. #include <QImage>
  5. #include <QQuickImageProvider>
  6.  
  7. class provedorImagem : public QQuickImageProvider
  8. {
  9. public:
  10. provedorImagem();
  11.  
  12. QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
  13.  
  14. void carregaImagem();
  15.  
  16. public slots:
  17. void carregaImagem(QImage imagemRecebida);
  18.  
  19. private:
  20. QImage imagem;
  21. };
  22.  
  23. #endif // PROVEDORIMAGEM_H
To copy to clipboard, switch view to plain text mode 

provedorimagem.cpp
Qt Code:
  1. #include "provedorimagem.h"
  2.  
  3. #include <QDebug>
  4.  
  5. provedorImagem::provedorImagem() : QQuickImageProvider(QQuickImageProvider::Image)
  6. {
  7.  
  8. }
  9.  
  10. QImage provedorImagem::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
  11. {
  12. if(imagem.isNull())
  13. {
  14. qDebug() << "Erro ao prover a imagem";
  15. }
  16. else
  17. {
  18. qDebug() << "Imagem provida";
  19. }
  20.  
  21. return imagem;
  22. }
  23.  
  24. void provedorImagem::carregaImagem(QImage imagemRecebida)
  25. {
  26. imagem = imagemRecebida;
  27. }
To copy to clipboard, switch view to plain text mode