1

I need to show a png image in a QML page using a QByteArray passed from c++ routines to QML.How can i do this.? Please help me with this guys..

Varghese Kiran
  • 95
  • 1
  • 1
  • 11

1 Answers1

0

Assuming you have your data in a QByteArray named data, this should work:

QImage image;
image.loadFromData(data);
const QImage swappedImage = image.rgbSwapped();
const bb::ImageData imageData = bb::ImageData::fromPixels(swappedImage.bits(), bb::PixelFormat::RGBA_Premultiplied, swappedImage.width(), swappedImage.height(), swappedImage.bytesPerLine());

_image = bb::cascades::Image(imageData);

You can now display _image in an ImageView in your QML code. To achieve this, you'll have to convert it in a QVariant: QVariant::fromValue(_image);. Once done, you can display it in any ImageView. I assume you have an object named feed which has an image property (QVariant):

ImageView {
    image: feed.image
}
Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
  • Thanks for your reply. How can i pass this _image to ImageView in QML.?I tried many method,but it all fails...!! – Varghese Kiran Jul 24 '13 at 04:18
  • I updated my answer to explain this. Basically, you have to convert your image in a ``QVariant``, then everything will just work. – Marc Plano-Lesay Jul 24 '13 at 11:54
  • Thanks for your reply.. One more help please.How can i set an image property (QVaiant) to the object named feed. – Varghese Kiran Jul 28 '13 at 05:56
  • My "feed" object is an object exported from C++. Its class declares different properties with the ``Q_PROPERTY`` macro: https://developer.blackberry.com/cascades/reference/properties.html – Marc Plano-Lesay Jul 28 '13 at 08:03
  • Will u please give me a sample code for the implementation of Q_PROPERTY for this scenario.Because the documentation is bit confusing. Thanks in Advance... – Varghese Kiran Jul 28 '13 at 13:08
  • My full ``Feed`` class is available here: https://github.com/Kernald/tt-rss-bb10/blob/master/src/data/feed.hpp - note that ``image`` is now ``icon``. – Marc Plano-Lesay Jul 28 '13 at 14:46
  • It did the magic.Thank you so much for helping me...:) – Varghese Kiran Jul 29 '13 at 06:36