0

I am using QQuickImageProvider and have taken a class object(PageBuffer) in requestimage function:

class ImageProvider : public QQuickImageProvider{
public:
explicit ImageProvider();
virtual QImage requestImage(int id, QSize *size, const QSize& requestedSize,PageBuffer p);
~ImageProvider();
 };

I want to set QImage of ImageProvider using the variable saved in the PageBuffer object and use the id as index Something like this:

QImage ImageProvider:: requestImage(int id, QSize *size, const QSize &requestedSize,PageBuffer p) {
return p.imagelist[id];
}

Here is my QML file where i want to call the Image Provider:

               Image{
                    x: 4
                    y: 4
                    height : imagerec.height
                    visible: true
                    width : imagerec.width
                    anchors.fill: imagerec
                    source:fileUrl
                    Text{
                        id:txt
                        x: 0
                        y: 71
                        text:"Sketch"+(index+1)
                        horizontalAlignment: txt.AlignHCenter
                        font.family: "Tahoma"
                        color:"#ffffff"

                    }

                    MouseArea {
                        anchors.rightMargin: -59
                        anchors.bottomMargin: -39
                        anchors.fill: parent
                        onClicked: {
                            p.index=index;
                            p.image=mod.get(index).fileUrl
                            main.source="image://image/"+index
                        }
                    }
                }
MJKhan
  • 61
  • 2
  • 7
  • 2
    So what is your question? – folibis Apr 27 '15 at 22:43
  • My question is how i should make this work , as it doesnt work like this – MJKhan Apr 28 '15 at 09:45
  • First of all you provided incompete code so it's impossible to test it. As I see you didn't register your image provider. See [QQmlEngine::addImageProvider](http://doc.qt.io/qt-5/qqmlengine.html#addImageProvider) to see how to do that. – folibis Apr 28 '15 at 09:57
  • Okay but can u tell me if my code is fine regarding the requestImage function? – MJKhan Apr 28 '15 at 10:38
  • And i have used AddImageProvider as well and it doesnt work, it gives unresolved external symbol error – MJKhan Apr 28 '15 at 11:00

1 Answers1

4

Ok guys, you can begin from this example by Qt. Also below the piece of code demonstrates using Image Provider to load random images from Internet, although I think that it is very trivial.

myimageprovider.h

class MyImageProvider : public QQuickImageProvider
{
public:
    MyImageProvider(ImageType type, Flags flags = 0);
    ~MyImageProvider();
    QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize);

protected:
    QNetworkAccessManager *manager;
};

myimageprovider.cpp

MyImageProvider::MyImageProvider(ImageType type, Flags flags) :
    QQuickImageProvider(type,flags)
{
    manager = new QNetworkAccessManager;
}

MyImageProvider::~MyImageProvider()
{
    delete manager;
}

QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    QUrl url("http://lorempixel.com/" + id);
    QNetworkReply* reply = manager->get(QNetworkRequest(url));
    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    if (reply->error() != QNetworkReply::NoError)
        return QImage();
    QImage image = QImage::fromData(reply->readAll());
    size->setWidth(image.width());
    size->setHeight(image.height());
    return image;
}

Registering our image provider in main.cpp:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    MyImageProvider *imageProvider = new MyImageProvider(QQmlImageProviderBase::Image);
    engine.addImageProvider("myprovider",imageProvider);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

And finally using the provider in QML:

Window {
    visible: true
    width: 800
    height: 600

    Image {
        id: img
        anchors.centerIn: parent
        source: "image://myprovider/500/500/"
        onStatusChanged: {
            if(status == Image.Ready)
                indicator.running = false;
        }

        BusyIndicator {
            id: indicator
            anchors.centerIn: parent
            running: false
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                indicator.running = true;
                img.source = "image://myprovider/500/500/?seed=" + Math.random(1000)
            }
        }
    }
}
folibis
  • 12,048
  • 6
  • 54
  • 97
  • If i have a context property object in QML and i need to send its Qimage variable to the image provider how should i do that ? – MJKhan Apr 29 '15 at 11:10
  • please answer this link: http://stackoverflow.com/questions/29929643/using-qimage-with-qquickimageprovider – MJKhan Apr 29 '15 at 11:10