[Solved] How QQuickImageProvider is dealing with http url links?



  • Hi everyone,

    I have created a QQuickImageProvider class in a *.h file like this:

    #ifndef IMAGEPROVIDER_H
    #define IMAGEPROVIDER_H

    #include <QObject>
    #include <QImage>
    #include <QQuickImageProvider>

    class ImageProvider : public QObject, public QQuickImageProvider
    {
    public:
    ImageProvider() : QQuickImageProvider( QQuickImageProvider::Image,
    QQuickImageProvider::ForceAsynchronousImageLoading )
    {

    }
    
    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    {
        return this->img;
    }
    
    QImage img;
    

    };

    #endif // IMAGEPROVIDER_H

    and after registering it in the main.cpp file, like this:

    qmlRegisterType<ImageProvider>( "com.example.ImageProvider", 1, 0, "ImageProvider" );
    QQmlApplicationEngine engine;
    engine.addImageProvider( QLatin1String("images"), new ImageProvider );
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

    I am trying to create a QML Image like this:

    Image {
    source: "image://images/http://static.giantbomb.com/uploads/scale_small/0/6393/528516-1ball2.jpg"
    }

    but then I get the error:

    QML Image: Failed to get image from provider: image://images/http://static.giantbomb.com/uploads/scale_small/0/6393/528516-1ball2.jpg


    My question is: how can I get an image from a url using the qquickimageprovider???

    Thanx!


  • Moderators

    Hi,

    Is it necessary for you to use QQuickImageProvider ?
    I mean you can directly use an http url as a source for Image element.



  • Thanks for your reply!

    I am now trying to a http url. However I have the following problem:
    The component is created before the download of the image is complete.
    Any idea?


  • Moderators

    bq. The component is created before the download of the image is complete.

    Yes since it may take some time for the image to load from the network.
    In case if you want to do operations when the image is completely loaded from the network you can use the "onStatusChanged":http://qt-project.org/doc/qt-5/qml-qtquick-image.html#status-prop handler and the current status of the progress.



  • Thank you very very much! That worked! I hadn't notice that. I was using the Component.onCompleted which was creating the rest items before download to be complete!

    Thanks again! That saved a lot of time and effort!


  • Moderators

    You're welcome :)
    You can mark the post as solved by editing the post title and prepend [solved] so that others may know that it has been solved.
    Happy Coding :)



  • Thnx for the advice!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.