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...
-
-
Or maybe you can just disable "caching":http://qt-project.org/doc/qt-4.8/qml-image.html#cache-prop in Image element