Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Wanted: QQuickImageProvider examples
QtWS25 Last Chance

[Solved] Wanted: QQuickImageProvider examples

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 13.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zamahra
    wrote on last edited by
    #1

    Hey Guys,

    I want to use QPixmaps in QML-Gui. For example:
    @interface->setProperty("image",QPixmap(path);@

    After some research I guess I should use QQuickImageProvider, but in the documentations the examples are missing: "QQuickImageProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html
    Does somebody know a good example to use it?

    Thank you very much!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomma
      wrote on last edited by
      #2

      Basically you implement one of
      @virtual QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize)
      virtual QPixmap requestPixmap(const QString & id, QSize * size, const QSize & requestedSize)
      virtual QQuickTextureFactory * requestTexture(const QString & id, QSize * size, const QSize & requestedSize)
      @
      Create your provider setting type to one you implemented and add to qmlengine:
      @QQmlEngine::addImageProvider(QLatin1String("idForYourProvider"), new YourProvider(type));@

      Then you can use it from Qml using "image" schema with your id:

      @Image { source: "image://idForYourProvider/imageFromYourProvider.png" }@

      Edit:
      Seems like 4.8 documentation has the example:
      http://qt-project.org/doc/qt-4.8/qdeclarativeimageprovider.html

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        Zamahra
        wrote on last edited by
        #3

        Thank you for your help. I wrote something like this (doesnt make sense, only wanted to test it):

        @#include <QColor>

        class ImageProvider : public QQuickImageProvider
        {
        public:
        ImageProvider()
        : QQuickImageProvider(QQuickImageProvider::Pixmap)
        {
        }

        QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
        {
            int width = 160;
            int height = 100;
        
            if(size)
                *size = QSize(width,height);
            QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
                           requestedSize.height() > 0 ? requestedSize.height() : height);
            pixmap.fill(QColor("blue").rgba());
            return pixmap;
        }
        

        };
        @

        Gui.cpp:
        @
        engine = new QQmlEngine;
        engine->addImageProvider(QLatin1String("provider"), new ImageProvider);
        viewer = new QQuickView;
        viewer->setSource(QUrl::fromLocalFile("qml/QML-MRGalleyServer/main.qml"));
        viewer->show();
        interface = viewer->rootObject();
        interface->setProperty("lastpicture7","image://provider/blue.png");
        @

        The property "lastpicture7" links to a source of an image. But I get following error: QML Image: Invalid image provider: image://provider/blue.png. Shoudnt it work this way? =( I used the 4.8 Example for QDeclarativeImageProvider

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomma
          wrote on last edited by
          #4

          You are not using engine you created at fist line. And also documentation says that you have to add image provider before loading qml so your code in Gui.cpp should be:

          @ viewer = new QQuickView;
          viewer->engine()->addImageProvider(QLatin1String("provider"), new ImageProvider);
          viewer->setSource(QUrl::fromLocalFile("qml/QML-MRGalleyServer/main.qml"));
          viewer->show();
          interface = viewer->rootObject();
          interface->setProperty("lastpicture7","image://provider/blue.png");@

          1 Reply Last reply
          0
          • F Offline
            F Offline
            favoritas37
            wrote on last edited by
            #5

            You could take a look at "this wiki page":http://www.developer.nokia.com/Community/Wiki/Multifunctional_Image_Tool:_A_base_for_image_interaction#Image_Handler.

            It could be helpful.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zamahra
              wrote on last edited by
              #6

              Thank you very much! The provider works perfect now. But i still have a little problem:

              The pixmaps provided by the ImageProvider have to change in runtime. So I call setProperty more than once. But when I change the property for the second time it doesnt call "requestPixmap". In the documentation it says:

              Image caching
              Images returned by a QDeclarativeImageProvider are automatically cached, similar to any image loaded by the QML engine. When an image with a "image://" prefix is loaded from cache, requestImage() and requestPixmap() will not be called for the relevant image provider. If an image should always be fetched from the image provider, and should not be cached at all, set the cache property to false for the relevant Image, BorderImage or AnimatedImage object.

              But I've got the same problems when I use @cache: false@
              for the relevant Image. Does anyone have an idea?

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zamahra
                wrote on last edited by
                #7

                Okay, I solved it by using random numbers in my id's. Seems like he is only updating via requestPixmap when the id has changed.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved