Results 1 to 4 of 4

Thread: Connecting QQuickImageProvider

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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
    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    guidupas (6th November 2015)

  4. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QGuiApplication app(argc, argv);
    4.  
    5. qmlRegisterType<processaImagem>("ProcessaImagemQml", 1, 0, "ProcessaImagem");
    6.  
    7. QQmlApplicationEngine engine;
    8.  
    9. provedorImagem *provedorImg = new provedorImagem;
    10.  
    11. //provedorImagem provedorImg;
    12.  
    13. //engine.rootContext()->setContextProperty("ProvedorImagem", provedorImg);
    14.  
    15. QObject *processadorImagem = engine.findChild<QObject*>("processadorImagem");
    16. QObject::connect(processadorImagem, SIGNAL(enviaImagem(QImage)), provedorImg, SLOT(carregaImagem(QImage)));
    17.  
    18. //------------A CHAVE É AQUI - CRIAR UMA CONEXÃO
    19.  
    20. engine.addImageProvider("provedor", provedorImg);
    21.  
    22. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Connecting Qt with PostgreSql
    By JonathanVeg in forum Newbie
    Replies: 10
    Last Post: 19th May 2012, 17:52
  2. QFtp not connecting?
    By budda in forum Newbie
    Replies: 1
    Last Post: 9th August 2011, 19:13
  3. Connecting different GUI sections
    By ajb in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2009, 11:47
  4. Connecting to my database
    By miguel_mark in forum Qt Programming
    Replies: 6
    Last Post: 22nd October 2007, 09:50
  5. Connecting to Oracle from QT App
    By KaptainKarl in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2007, 08:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.