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



  • 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?



  • 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 .



  • 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...



  • 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.



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



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


Log in to reply
 

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