November 9, 2010

fcrochik fcrochik
Lab Rat
517 posts

[SOLVED] How to create QML Image Element from a QImage Object (C++)?

 

How can I convert a QImage into a QML Element? I can’t use the Image QML Element because the source property requires an URL and I only have the actual QImage in memory.

The only way I could come up with so far is to extend the QDeclarativeItem and render the image. It is far from ideal because then I can’t make use of the capabilities of the QML Image element.

Thanks,
Felipe

 Signature 

Certified Specialist & Qt Ambassador Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop… Qt everywhere!

5 replies

November 9, 2010

mbrasser mbrasser
Lab Rat
452 posts

Hi,

You can use QDeclarativeImageProvider [doc.qt.nokia.com] to load a QImage into an Image element.

Regards,
Michael

November 9, 2010

fcrochik fcrochik
Lab Rat
517 posts

Michael,
Thanks, it worked. It is not as straight forward as I would expect for something that looks like a very common scenario (e.g. a model view with QImage returned for the DecorationRole) but will do.
Felipe

 Signature 

Certified Specialist & Qt Ambassador Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop… Qt everywhere!

November 10, 2010

mbrasser mbrasser
Lab Rat
452 posts

Yes, it’s less than ideal. Originally we allowed setting a QPixmap directly on the Image element, but there were memory management issues with that approach. QDeclarativeImageProvider was the solution we came up with to those issues for 4.7 — it may be that a better solution is possible in future releases.

Please let us know if you have any suggestions.

November 10, 2010

fcrochik fcrochik
Lab Rat
517 posts

Michael,
Unfortunately I don’t know the details about how Qt manage memory so can’t even guess the best way to implement but I can think of two user case scenarios:

1. When you have a “legacy” model with images, you would want to initialize the Image Element with the QImage doing something hopefully as simple as this:

  1. Image {
  2.    sourceX: modelData.decorationRole;
  3. }

2. On a little more sophisticated user case you could have a c++ class like:

  1. class A : public QObject {
  2.   Q_OBJECT
  3.   Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
  4. ...
  5. }

set the context property:

  1. ui->view->rootContext()->setContextProperty("myA", new A());

and then you could bind the QImage on QML:

  1. Image {
  2.    sourceWithBinding: myA.image;
  3. }

so the QML Image element could be notified of changes on the property (for example as result of the C++ app generating a new graph as result of the user input)

 Signature 

Certified Specialist & Qt Ambassador Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop… Qt everywhere!

August 30, 2011

fdelgado fdelgado
Lab Rat
1 posts

It’s maybe too late but I wrote an article about this and it can maybe help someone.

http://www.fdelgado.fr/QtQuick/2011/08/30/72/ [fdelgado.fr]

It’s in french but easily too translate with tools, and the code can be usefull.

 
  ‹‹ Best way to populate/update/delete info from a list(ListView/ListModel) from C++      #ifdef MAC / Win / Linux in QML ››

You must log in to post a reply. Not a member yet? Register here!