I am trying to implement it with QQuickImageProvider but I am not understanding why it is not working. My code is below. If you could help me.

.h
Qt Code:
  1. #ifndef MANIPULAIMAGEM_H
  2. #define MANIPULAIMAGEM_H
  3.  
  4. #include <QObject>
  5. #include <QImage>
  6. #include <QQuickImageProvider>
  7. #include <QQmlEngine>
  8. #include <QQmlContext>
  9.  
  10. class manipulaImagem : public QObject, public QQuickImageProvider
  11. {
  12. Q_OBJECT
  13.  
  14. public slots:
  15. QString recortarFotoPerfil(const QString &imagem, QRect rectRecorte);
  16.  
  17. public:
  18. manipulaImagem(QObject *parent = 0);
  19.  
  20. QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
  21.  
  22. private:
  23. void alocaImagem(const QString &imagem, QRect rectRecorte);
  24.  
  25. QImage imagemEditada;
  26. };
  27.  
  28. #endif // MANIPULAIMAGEM_H
To copy to clipboard, switch view to plain text mode 

.cpp
Qt Code:
  1. #include "manipulaimagem.h"
  2.  
  3. #include <QDebug>
  4.  
  5. manipulaImagem::manipulaImagem(QObject *parent) : QQuickImageProvider(QQmlImageProviderBase::Image)
  6. {
  7.  
  8. }
  9.  
  10. QImage manipulaImagem::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
  11. {
  12. if(imagemEditada.isNull())
  13. {
  14. qDebug() << "Request image: (image is null)";
  15. }
  16. else
  17. {
  18. qDebug() << "Request image: image is OK";
  19. }
  20.  
  21. return imagemEditada;
  22. }
  23.  
  24. void manipulaImagem::alocaImagem(const QString &imagem, QRect rectRecorte)
  25. {
  26. QUrl caminhoImagem(imagem);
  27. QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
  28. QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
  29. QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
  30.  
  31. QSize imageSize;
  32. QString imageId = caminhoImagem.path().remove(0, 1);
  33. imagemEditada = imageProvider->requestImage(imageId, &imageSize, imageSize);
  34.  
  35. if(imagemEditada.isNull())
  36. {
  37. qDebug() << "Loading image failed";
  38. }
  39. else
  40. {
  41. qDebug() << "Loading image OK";
  42. }
  43. }
  44.  
  45. QString manipulaImagem::recortarFotoPerfil(const QString &imagem, QRect rectRecorte)
  46. {
  47. this->alocaImagem(imagem, rectRecorte);
  48.  
  49. QString a = "image://ProvedorImagens/imagemEditada";
  50.  
  51. if(imagemEditada.isNull())
  52. {
  53. qDebug() << "Imagem is null";
  54. }
  55. else
  56. {
  57. qDebug() << "Imagem is loaded";
  58. }
  59.  
  60. return a;
  61. }
To copy to clipboard, switch view to plain text mode 

.qml
Qt Code:
  1. ManipulaImagem {
  2. id: manipulaImagem
  3. }
  4.  
  5. Camera {
  6. id: camera
  7.  
  8. captureMode: Camera.CaptureStillImage
  9.  
  10. imageCapture {
  11. onImageCaptured: {
  12. previewImage.source = manipulaImagem.recortarFotoPerfil(preview, viewfinder.mapRectToSource(Qt.rect(viewfinder.x, viewfinder.y, viewfinder.width, viewfinder.height)));
  13. }
  14. }
  15. }
  16.  
  17. Rectangle {
  18. id: previewRectangle
  19.  
  20. visible: false
  21.  
  22. anchors.fill: parent
  23.  
  24. Image {
  25. id: previewImage
  26.  
  27. fillMode: Image.PreserveAspectFit
  28.  
  29. anchors.top: parent.top
  30.  
  31. width: parent.width
  32. height: parent.width
  33. }
To copy to clipboard, switch view to plain text mode 

The output of this code is:

Loading image OK

Imagem is loaded

Request image: (image is null)

QML Image: Failed to get image from provider: image://provedorimagens/imagemEditada

What happens is that when I call the functions the image is not null, but when I try to return the QImage using the provider it cant return the image. I dont know why but for the image provider the image is null.

How could I solve that?