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. Passing QByteArray image from c++ to QML [Solved]

Passing QByteArray image from c++ to QML [Solved]

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 10.0k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,
    need to show a jpg image in a QML page using a QByteArray passed from c++ routines to QML.

    "caricaImmagineTelecamera" is the slot in my QML that is connected to c++ signal an that receive the QByteArray:
    @

    Image {
    id: img
    source: ?????

    }

    function caricaImmagineTelecamera(img_telecamere_ba)
    {
    ?????
    }
    @

    How can I show img_telecamere_ba image in the "img" element?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      You can't use QByteArray or QImage or QPixmap for image source in QML. QML Image element supports only QUrl sources - it can be local file, file on remote filesystem(samba, webdav), file on the web and file from resource. If you didn't have possibility to save your QByteArray as an image in the temp folder and use the path with QUrl::fromLocalfile(), you should then take a look on "QDeclarativeImageProvider":http://qt-project.org/doc/qt-4.8/qdeclarativeimageprovider.html .

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        Ok, thanks.

        This is my ImageProvider:
        @
        #include "imageprovider.h"

        ImageProvider::ImageProvider()
        :QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
        {

        }

        QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
        {
        QImage image;
        image = QImage::fromData(m_baImmagine);
        *size = image.size();
        return image;
        }

        void ImageProvider::caricaImmagineTelecamera(QByteArray ba_immagine)
        {
        m_baImmagine = ba_immagine;
        }

        @

        I can load the image in qml using:
        @
        Image {
        id: img
        source:"image://telecamera/immagine.jpg"
        }
        @

        Now the problem is that when I update in C++ my m_baImmagine it seems Image doesn't update the showed image even if I call:
        @
        img.source="image://telecamera/immagine.jpg"
        @

        It doesn't show the new image...

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          Ok, I solved adding a random number to image source:

          @
          img.source="image://telecamera/immagine.jpg1234"
          img.source="image://telecamera/immagine.jpg1235"
          img.source="image://telecamera/immagine.jpg1444"
          @

          This way I get an updated image every time...

          Thanks.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            Or maybe you can just disable "caching":http://qt-project.org/doc/qt-4.8/qml-image.html#cache-prop in Image element

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on last edited by
              #6

              I tried to set "cache:false" but it doesn't solve problem.
              I need to change source name to get updated image.

              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