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. [SOLVED] How to create QML Image Element from a QImage Object (C++)?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 17.3k Views
  • 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.
  • F Offline
    F Offline
    fcrochik
    wrote on last edited by
    #1

    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

    Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      Hi,

      You can use "QDeclarativeImageProvider":http://doc.qt.nokia.com/4.7/qdeclarativeimageprovider.html to load a QImage into an Image element.

      Regards,
      Michael

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fcrochik
        wrote on last edited by
        #3

        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

        Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fcrochik
            wrote on last edited by
            #5

            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:

            @Image {
            sourceX: modelData.decorationRole;
            }
            @

            1. On a little more sophisticated user case you could have a c++ class like:
              @class A : public QObject {
              Q_OBJECT
              Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
              ...
              }@

            set the context property:

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

            and then you could bind the QImage on QML:

            @Image {
            sourceWithBinding: myA.image;
            }@

            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)

            Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fdelgado
              wrote on last edited by
              #6

              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/":http://www.fdelgado.fr/QtQuick/2011/08/30/72/

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

              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